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

export const PARTIAL_TASK_FIELDS = gql`
  fragment TaskFields on tasks {
    id
    created_at
    updated_at
    title
    description
    deleted
    deleted_at
    due_date
    created_by
    assigned_to
    assigned_to_employee {
      id
      user_email
    }
    completed
    completed_at
    remind_at
    priority
    job {
      id
      ro_number
      joblines {
        id
        line_desc
      }
      bills {
        id
        vendor {
          id
          name
        }
        invoice_number
      }
      parts_orders {
        id
        vendor {
          id
          name
        }
        order_number
      }
    }
    jobid
    jobline {
      id
      line_desc
    }
    joblineid
    parts_order {
      id
      vendor {
        id
        name
      }
      order_number
    }
    partsorderid
    bill {
      id
      vendor {
        id
        name
      }
      invoice_number
    }
    billid
  }
`;

export const PARTIAL_TASK_CENTER_FIELDS = gql`
  fragment PartialTaskFields on tasks {
    id
    title
    description
    due_date
    priority
    jobid
    job {
      ro_number
    }
    joblineid
    partsorderid
    billid
    remind_at
    created_at
    assigned_to
    bodyshopid
    deleted
    completed
  }
`;

export const QUERY_TASKS_WITH_DUE_DATES = gql`
  ${PARTIAL_TASK_CENTER_FIELDS}
  query QUERY_TASKS_WITH_DUE_DATES($bodyshop: uuid!, $assigned_to: uuid!, $order: [tasks_order_by!]!) {
    tasks(
      where: {
        bodyshopid: { _eq: $bodyshop }
        assigned_to: { _eq: $assigned_to }
        deleted: { _eq: false }
        completed: { _eq: false }
        due_date: { _is_null: false }
      }
      order_by: $order
    ) {
      ...PartialTaskFields
    }
  }
`;
export const QUERY_TASKS_NO_DUE_DATE_PAGINATED = gql`
  ${PARTIAL_TASK_CENTER_FIELDS}
  query QUERY_TASKS_NO_DUE_DATE_PAGINATED(
    $bodyshop: uuid!
    $assigned_to: uuid!
    $order: [tasks_order_by!]!
    $limit: Int!
    $offset: Int!
  ) {
    tasks(
      where: {
        bodyshopid: { _eq: $bodyshop }
        assigned_to: { _eq: $assigned_to }
        deleted: { _eq: false }
        completed: { _eq: false }
        due_date: { _is_null: true }
      }
      order_by: $order
      limit: $limit
      offset: $offset
    ) {
      ...PartialTaskFields
    }
    tasks_aggregate(
      where: {
        bodyshopid: { _eq: $bodyshop }
        assigned_to: { _eq: $assigned_to }
        deleted: { _eq: false }
        completed: { _eq: false }
        due_date: { _is_null: true }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;
/**
 * Query to get the count of my tasks
 * @type {DocumentNode}
 */
export const QUERY_MY_TASKS_COUNT = gql`
  query QUERY_MY_TASKS_COUNT($assigned_to: uuid!, $bodyshopid: uuid!) {
    tasks_aggregate(
      where: {
        assigned_to: { _eq: $assigned_to }
        bodyshopid: { _eq: $bodyshopid }
        completed: { _eq: false }
        deleted: { _eq: false }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

export const QUERY_GET_TASK_BY_ID = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_GET_TASK_BY_ID($id: uuid!) {
    tasks_by_pk(id: $id) {
      ...TaskFields
    }
  }
`;

export const QUERY_ALL_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_ALL_TASKS_PAGINATED(
    $offset: Int
    $limit: Int
    $bodyshop: uuid!
    $deleted: Boolean
    $completed: Boolean
    $assigned_to: uuid
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

// Query for joblineid
export const QUERY_JOBLINE_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_JOBLINE_TASKS_PAGINATED(
    $offset: Int
    $limit: Int
    $joblineid: uuid!
    $bodyshop: uuid!
    $deleted: Boolean
    $completed: Boolean
    $assigned_to: uuid
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        joblineid: { _eq: $joblineid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        joblineid: { _eq: $joblineid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

// Query for partsorderid
export const QUERY_PARTSORDER_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_PARTSORDER_TASKS_PAGINATED(
    $offset: Int
    $limit: Int
    $partsorderid: uuid!
    $bodyshop: uuid!
    $deleted: Boolean
    $completed: Boolean
    $assigned_to: uuid
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        partsorderid: { _eq: $partsorderid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        partsorderid: { _eq: $partsorderid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

// Query for billid
export const QUERY_BILL_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_BILL_TASKS_PAGINATED(
    $offset: Int
    $limit: Int
    $billid: uuid!
    $bodyshop: uuid!
    $deleted: Boolean
    $completed: Boolean
    $assigned_to: uuid
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        billid: { _eq: $billid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        billid: { _eq: $billid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

// Use the fragment in your queries
export const QUERY_JOB_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_JOB_TASKS_PAGINATED(
    $offset: Int
    $limit: Int
    $jobid: uuid!
    $bodyshop: uuid!
    $deleted: Boolean
    $completed: Boolean
    $assigned_to: uuid
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        jobid: { _eq: $jobid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        jobid: { _eq: $jobid }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        assigned_to: { _eq: $assigned_to }
        completed: { _eq: $completed }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

export const QUERY_MY_ACTIVE_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_MY_ACTIVE_TASKS_PAGINATED(
    $assigned_to: uuid!
    $bodyshop: uuid!
    $offset: Int
    $limit: Int
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        assigned_to: { _eq: $assigned_to }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: false }
        completed: { _eq: false }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        assigned_to: { _eq: $assigned_to }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: false }
        completed: { _eq: false }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

export const QUERY_MY_TASKS_PAGINATED = gql`
  ${PARTIAL_TASK_FIELDS}
  query QUERY_MY_TASKS_PAGINATED(
    $offset: Int
    $limit: Int
    $assigned_to: uuid!
    $bodyshop: uuid!
    $deleted: Boolean
    $completed: Boolean
    $order: [tasks_order_by!]!
  ) {
    tasks(
      offset: $offset
      limit: $limit
      order_by: $order
      where: {
        assigned_to: { _eq: $assigned_to }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        completed: { _eq: $completed }
      }
    ) {
      ...TaskFields
    }
    tasks_aggregate(
      where: {
        assigned_to: { _eq: $assigned_to }
        bodyshopid: { _eq: $bodyshop }
        deleted: { _eq: $deleted }
        completed: { _eq: $completed }
      }
    ) {
      aggregate {
        count
      }
    }
  }
`;

/**
 * Toggle task completed mutation
 * @type {DocumentNode}
 */
export const MUTATION_TOGGLE_TASK_COMPLETED = gql`
  ${PARTIAL_TASK_FIELDS}
  mutation MUTATION_TOGGLE_TASK_COMPLETED($id: uuid!, $completed: Boolean!, $completed_at: timestamptz) {
    update_tasks_by_pk(pk_columns: { id: $id }, _set: { completed: $completed, completed_at: $completed_at }) {
      ...TaskFields
    }
  }
`;

/**
 * Toggle task deleted mutation
 * @type {DocumentNode}
 */
export const MUTATION_TOGGLE_TASK_DELETED = gql`
  ${PARTIAL_TASK_FIELDS}
  mutation MUTATION_TOGGLE_TASK_DELETED($id: uuid!, $deleted: Boolean!, $deleted_at: timestamptz) {
    update_tasks_by_pk(pk_columns: { id: $id }, _set: { deleted: $deleted, deleted_at: $deleted_at }) {
      ...TaskFields
    }
  }
`;

/**
 * Insert new task mutation
 * @type {DocumentNode}
 */
export const MUTATION_INSERT_NEW_TASK = gql`
  ${PARTIAL_TASK_FIELDS}
  mutation MUTATION_INSERT_NEW_TASK($taskInput: [tasks_insert_input!]!) {
    insert_tasks(objects: $taskInput) {
      returning {
        ...TaskFields
      }
    }
  }
`;

/**
 * Update task mutation
 * @type {DocumentNode}
 */
export const MUTATION_UPDATE_TASK = gql`
  ${PARTIAL_TASK_FIELDS}
  mutation MUTATION_UPDATE_TASK($taskId: uuid!, $task: tasks_set_input!) {
    update_tasks(where: { id: { _eq: $taskId } }, _set: $task) {
      returning {
        ...TaskFields
      }
    }
  }
`;
