// GraphQL Queries and Mutations
const GET_BODYSHOP_STATUS = `
  query GetBodyshopStatus($id: uuid!) {
    bodyshops_by_pk(id: $id) {
      md_ro_statuses
    }
  }
`;

const GET_VEHICLE_BY_SHOP_VIN = `
  query GetVehicleByShopVin($shopid: uuid!, $v_vin: String!) {
    vehicles(where: { shopid: { _eq: $shopid }, v_vin: { _eq: $v_vin } }, limit: 1) {
      id
    }
  }
`;

const INSERT_OWNER = `
  mutation InsertOwner($owner: owners_insert_input!) {
    insert_owners_one(object: $owner) {
      id
    }
  }
`;

const INSERT_JOB_WITH_LINES = `
  mutation InsertJob($job: jobs_insert_input!) {
    insert_jobs_one(object: $job) {
      id
      joblines { id unq_seq }
    }
  }
`;

const GET_JOB_BY_CLAIM = `
  query GetJobByClaim($shopid: uuid!, $clm_no: String!) {
    jobs(
      where: { shopid: { _eq: $shopid }, clm_no: { _eq: $clm_no } }
      order_by: { created_at: desc }
      limit: 1
    ) {
      id
    }
  }
`;

const GET_JOB_BY_ID = `
  query GetJobByID($shopid: uuid!, $jobid: uuid!) {
    jobs(
      where: { shopid: { _eq: $shopid }, id: { _eq: $jobid } }
      order_by: { created_at: desc }
      limit: 1
    ) {
      id
    }
  }
`;

const UPDATE_JOB_BY_ID = `
  mutation UpdateJobById($id: uuid!, $job: jobs_set_input!) {
    update_jobs_by_pk(pk_columns: { id: $id }, _set: $job) {
      id
    }
  }
`;

// Soft delete joblines by marking removed=true instead of hard-deleting
const SOFT_DELETE_JOBLINES_BY_IDS = `
  mutation SoftDeleteJoblinesByIds($jobid: uuid!, $unqSeqs: [Int!]!) {
    update_joblines(
      where: { jobid: { _eq: $jobid }, unq_seq: { _in: $unqSeqs } },
      _set: { removed: true }
    ) {
      affected_rows
    }
  }
`;

const INSERT_JOBLINES = `
  mutation InsertJoblines($joblines: [joblines_insert_input!]!) {
    insert_joblines(objects: $joblines) {
      affected_rows
    }
  }
`;

const CHECK_EXTERNAL_SHOP_ID = `
  query CHECK_KEY($key: String!) {
    bodyshops(where: { external_shop_id: { _eq: $key } }) {
      external_shop_id
    }
  }
`;

const CREATE_SHOP = `
  mutation CREATE_SHOP($bs: bodyshops_insert_input!) {
    insert_bodyshops_one(object: $bs) { id }
  }
`;

const DELETE_VENDORS_BY_SHOP = `
  mutation DELETE_VENDORS($shopId: uuid!) {
    delete_vendors(where: { bodyshopid: { _eq: $shopId } }) {
      affected_rows
    }
  }
`;

const DELETE_SHOP = `
  mutation DELETE_SHOP($id: uuid!) {
    delete_bodyshops_by_pk(id: $id) { id }
  }
`;

const CREATE_USER = `
  mutation CREATE_USER($u: users_insert_input!) {
    insert_users_one(object: $u) {
      id: authid
      email
    }
  }
`;

const GET_BODYSHOP = `
  query GetBodyshop($id: uuid!) {
    bodyshops_by_pk(id: $id) {
      external_shop_id
      shopname
    }
  }
`;

const GET_ASSOCIATED_USERS = `
  query GetAssociatedUsers($shopId: uuid!) {
    associations(where: {shopid: {_eq: $shopId}}) {
      user {
        authid
        email
      }
    }
  }
`;

const DELETE_ASSOCIATIONS_BY_SHOP = `
  mutation DeleteAssociationsByShop($shopId: uuid!) {
    delete_associations(where: {shopid: {_eq: $shopId}}) {
      affected_rows
    }
  }
`;

const GET_USER_ASSOCIATIONS_COUNT = `
  query GetUserAssociationsCount($userEmail: String!) {
    associations_aggregate(where: {useremail: {_eq: $userEmail}}) {
      aggregate {
        count
      }
    }
  }
`;

const DELETE_USER = `
  mutation DeleteUser($email: String!) {
    delete_users(where: {email: {_eq: $email}}) {
      affected_rows
    }
  }
`;

const GET_VENDORS = `
  query GetVendors($shopId: uuid!) {
    vendors(where: {bodyshopid: {_eq: $shopId}}) {
      name
    }
  }
`;

const GET_JOBS_BY_SHOP = `
  query GetJobsByShop($shopId: uuid!) {
    jobs(where: {shopid: {_eq: $shopId}}) {
      id
    }
  }
`;

const DELETE_JOBLINES_BY_JOB_IDS = `
  mutation DeleteJoblinesByJobIds($jobIds: [uuid!]!) {
    delete_joblines(where: {jobid: {_in: $jobIds}}) {
      affected_rows
    }
  }
`;

const DELETE_JOBS_BY_IDS = `
  mutation DeleteJobsByIds($jobIds: [uuid!]!) {
    delete_jobs(where: {id: {_in: $jobIds}}) {
      affected_rows
    }
  }
`;

const DELETE_AUDIT_TRAIL_BY_SHOP = `
  mutation DeleteAuditTrailByShop($shopId: uuid!) {
    delete_audit_trail(where: {bodyshopid: {_eq: $shopId}}) {
      affected_rows
    }
  }
`;

const GET_JOBLINES_NOTES_BY_JOBID_UNQSEQ = `
  query GetJoblinesNotesByJobIdUnqSeq($jobid: uuid!, $unqSeqs: [Int!]!) {
    joblines(where: { jobid: { _eq: $jobid }, unq_seq: { _in: $unqSeqs }, removed: { _neq: true } }) {
      unq_seq
      notes
    }
  }
`;

// Clear task links to parts orders for all jobs in a shop to avoid FK violations when deleting parts orders
const CLEAR_TASKS_PARTSORDER_LINKS_BY_JOBIDS = `
  mutation ClearTasksPartsOrderLinks($jobIds: [uuid!]!) {
    update_tasks(
      where: { parts_order: { jobid: { _in: $jobIds } } },
      _set: { partsorderid: null }
    ) {
      affected_rows
    }
  }
`;

// Delete parts order lines where the parent order belongs to any of the provided job IDs
const DELETE_PARTS_ORDER_LINES_BY_JOB_IDS = `
  mutation DeletePartsOrderLinesByJobIds($jobIds: [uuid!]!) {
    delete_parts_order_lines(where: { parts_order: { jobid: { _in: $jobIds } } }) {
      affected_rows
    }
  }
`;

// Delete parts orders for the given job IDs
const DELETE_PARTS_ORDERS_BY_JOB_IDS = `
  mutation DeletePartsOrdersByJobIds($jobIds: [uuid!]!) {
    delete_parts_orders(where: { jobid: { _in: $jobIds } }) {
      affected_rows
    }
  }
`;

const UPSERT_JOBLINES = `
  mutation UpsertJoblines($joblines: [joblines_insert_input!]!) {
    insert_joblines(
      objects: $joblines,
      on_conflict: {
        constraint: joblines_jobid_unq_seq_key,
        update_columns: [
          status,
          line_desc,
          notes,
          manual_line,
          part_qty,
          oem_partno,
          alt_partno,
          part_type,
          act_price,
          db_price,
          tax_part,
          mod_lbr_ty,
          mod_lb_hrs,
          op_code_desc,
          lbr_amt,
          lbr_typ_j,
          lbr_hrs_j,
          lbr_op_j,
          paint_stg,
          paint_tone
        ]
      }
    ) {
      affected_rows
    }
  }
`;

// Get jobline IDs for the incoming unq_seq values (only non-removed)
const GET_JOBLINE_IDS_BY_JOBID_UNQSEQ = `
  query GetJoblineIdsByJobIdUnqSeq($jobid: uuid!, $unqSeqs: [Int!]!) {
    joblines(where: { jobid: { _eq: $jobid }, unq_seq: { _in: $unqSeqs }, removed: { _neq: true } }) {
      id
      unq_seq
    }
  }
`;

// Update a single jobline by primary key
const UPDATE_JOBLINE_BY_PK = `
  mutation UpdateJoblineByPk($id: uuid!, $jl: joblines_set_input!) {
    update_joblines_by_pk(pk_columns: { id: $id }, _set: $jl) { id }
  }
`;

const UPDATE_BODYSHOP_BY_ID = `
  mutation UpdateBodyshopById($id: uuid!, $fields: bodyshops_set_input!) {
    update_bodyshops_by_pk(pk_columns: { id: $id }, _set: $fields) {
      id
      shopname
      address1
      address2
      city
      state
      zip_post
      country
      email
      timezone
      phone
      logo_img_path
    }
  }
`;

module.exports = {
  GET_BODYSHOP_STATUS,
  GET_VEHICLE_BY_SHOP_VIN,
  INSERT_OWNER,
  INSERT_JOB_WITH_LINES,
  GET_JOB_BY_CLAIM,
  UPDATE_JOB_BY_ID,
  SOFT_DELETE_JOBLINES_BY_IDS,
  INSERT_JOBLINES,
  CHECK_EXTERNAL_SHOP_ID,
  CREATE_SHOP,
  DELETE_VENDORS_BY_SHOP,
  DELETE_SHOP,
  CREATE_USER,
  GET_BODYSHOP,
  GET_ASSOCIATED_USERS,
  DELETE_ASSOCIATIONS_BY_SHOP,
  GET_USER_ASSOCIATIONS_COUNT,
  DELETE_USER,
  GET_VENDORS,
  GET_JOBS_BY_SHOP,
  DELETE_JOBLINES_BY_JOB_IDS,
  DELETE_JOBS_BY_IDS,
  DELETE_AUDIT_TRAIL_BY_SHOP,
  GET_JOBLINES_NOTES_BY_JOBID_UNQSEQ,
  GET_JOB_BY_ID,
  CLEAR_TASKS_PARTSORDER_LINKS_BY_JOBIDS,
  DELETE_PARTS_ORDER_LINES_BY_JOB_IDS,
  DELETE_PARTS_ORDERS_BY_JOB_IDS,
  UPSERT_JOBLINES,
  GET_JOBLINE_IDS_BY_JOBID_UNQSEQ,
  UPDATE_JOBLINE_BY_PK,
  UPDATE_BODYSHOP_BY_ID
};
