import { gql } from "@apollo/client";

export const QUERY_SHOP_ASSOCIATIONS = gql`
  query QUERY_SHOP_ASSOCIATIONS($shopid: uuid!) {
    associations(where: { shopid: { _eq: $shopid } }) {
      id
      authlevel
      new_message_sound
      shopid
      user {
        email
      }
    }
  }
`;

export const UPDATE_ASSOCIATION = gql`
  mutation UPDATE_ASSOCIATION($assocId: uuid!, $assoc: associations_set_input!) {
    update_associations(where: { id: { _eq: $assocId } }, _set: $assoc) {
      returning {
        id
        authlevel
        shopid
        user {
          email
        }
      }
    }
  }
`;

// Query to load the active association for a given user and get the new_message_sound flag
export const QUERY_ACTIVE_ASSOCIATION_SOUND = gql`
  query QUERY_ACTIVE_ASSOCIATION_SOUND($email: String!) {
    associations(where: { _and: { useremail: { _eq: $email }, active: { _eq: true } } }) {
      id
      new_message_sound
    }
  }
`;

// Mutation to update just the new_message_sound field
export const UPDATE_NEW_MESSAGE_SOUND = gql`
  mutation UPDATE_NEW_MESSAGE_SOUND($id: uuid!, $value: Boolean) {
    update_associations_by_pk(pk_columns: { id: $id }, _set: { new_message_sound: $value }) {
      id
      new_message_sound
    }
  }
`;

export const INSERT_EULA_ACCEPTANCE = gql`
  mutation INSERT_EULA_ACCEPTANCE($eulaAcceptance: eula_acceptances_insert_input!) {
    insert_eula_acceptances_one(object: $eulaAcceptance) {
      id
    }
  }
`;

export const UPSERT_USER = gql`
  mutation UPSERT_USER($authEmail: String!, $authToken: String!) {
    insert_users(
      objects: [{ email: $authEmail, authid: $authToken }]
      on_conflict: { constraint: users_pkey, update_columns: [authid] }
    ) {
      returning {
        authid
      }
    }
  }
`;

export const UPDATE_DASHBOARD_LAYOUT = gql`
  mutation UPDATE_DASHBOARD_LAYOUT($email: String!, $layout: jsonb!) {
    update_users_by_pk(pk_columns: { email: $email }, _set: { dashboardlayout: $layout }) {
      email
      dashboardlayout
    }
  }
`;

export const UPDATE_FCM_TOKEN = gql`
  mutation UPDATE_FCM_TOKEN($authEmail: String!, $token: jsonb!) {
    update_users(where: { email: { _eq: $authEmail } }, _append: { fcmtokens: $token }) {
      affected_rows
      returning {
        fcmtokens
      }
    }
  }
`;

export const QUERY_KANBAN_SETTINGS = gql`
  query QUERY_KANBAN_SETTINGS($email: String!) {
    associations(where: { _and: { useremail: { _eq: $email }, active: { _eq: true } } }) {
      id
      kanban_settings
    }
  }
`;

export const UPDATE_KANBAN_SETTINGS = gql`
  mutation UPDATE_KANBAN_SETTINGS($id: uuid!, $ks: jsonb) {
    update_associations_by_pk(pk_columns: { id: $id }, _set: { kanban_settings: $ks }) {
      id
      kanban_settings
    }
  }
`;

export const QUERY_NOTIFICATION_SETTINGS = gql`
  query QUERY_NOTIFICATION_SETTINGS($email: String!) {
    associations(where: { _and: { useremail: { _eq: $email }, active: { _eq: true } } }) {
      id
      notification_settings
      notifications_autoadd
    }
  }
`;

export const UPDATE_NOTIFICATION_SETTINGS = gql`
  mutation UPDATE_NOTIFICATION_SETTINGS($id: uuid!, $ns: jsonb) {
    update_associations_by_pk(pk_columns: { id: $id }, _set: { notification_settings: $ns }) {
      id
      notification_settings
    }
  }
`;

export const UPDATE_NOTIFICATIONS_AUTOADD = gql`
  mutation UPDATE_NOTIFICATIONS_AUTOADD($id: uuid!, $autoadd: Boolean!) {
    update_associations_by_pk(pk_columns: { id: $id }, _set: { notifications_autoadd: $autoadd }) {
      id
      notifications_autoadd
    }
  }
`;

export const QUERY_USER_DASHBOARD_LAYOUT = gql`
  query QUERY_USER_DASHBOARD_LAYOUT($email: String!) {
    users(where: { email: { _eq: $email } }) {
      email
      dashboardlayout
    }
  }
`;
