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

export const QUERY_EMPLOYEES = gql`
  query QUERY_EMPLOYEES {
    employees(order_by: { employee_number: asc }) {
      active
      employee_number
      first_name
      flat_rate
      id
      last_name
    }
  }
`;
export const QUERY_EMPLOYEE_BY_ID = gql`
  query QUERY_EMPLOYEE_BY_ID($id: uuid!) {
    employees_by_pk(id: $id) {
      last_name
      id
      first_name
      employee_number
      active
      termination_date
      hire_date
      flat_rate
      rates
      pin
      user_email
      external_id
      employee_vacations(order_by: { start: desc }) {
        id
        start
        end
      }
    }
  }
`;

export const CHECK_EMPLOYEE_NUMBER = gql`
  query CHECK_EMPLOYEE_NUMBER($employeenumber: String!) {
    employees_aggregate(where: { employee_number: { _ilike: $employeenumber } }) {
      aggregate {
        count
      }
      nodes {
        id
      }
    }
  }
`;

export const QUERY_ACTIVE_EMPLOYEES = gql`
  query QUERY_ACTIVE_EMPLOYEES {
    employees(where: { active: { _eq: true } }) {
      last_name
      id
      first_name
      employee_number
      active
      termination_date
      hire_date
      flat_rate
      rates
      pin
      user_email
    }
  }
`;

export const QUERY_ACTIVE_EMPLOYEES_WITH_EMAIL = gql`
  query QUERY_ACTIVE_EMPLOYEES_WITH_EMAIL {
    employees(where: {active: {_eq: true}, user_email: {_is_null: false}}) {
      last_name
      id
      first_name
      employee_number
      active
      termination_date
      hire_date
      flat_rate
      rates
      pin
      user_email
    }
  }
`;

export const INSERT_EMPLOYEES = gql`
  mutation INSERT_EMPLOYEES($employees: [employees_insert_input!]!) {
    insert_employees(objects: $employees) {
      returning {
        last_name
        id
        first_name
        employee_number
        active
        termination_date
        hire_date
        flat_rate
        rates
        pin
        user_email
      }
    }
  }
`;

export const UPDATE_EMPLOYEE = gql`
  mutation UPDATE_EMPLOYEE($id: uuid!, $employee: employees_set_input) {
    update_employees(where: { id: { _eq: $id } }, _set: $employee) {
      returning {
        last_name
        id
        first_name
        employee_number
        active
        termination_date
        hire_date
        flat_rate
        rates
        pin
        user_email
      }
    }
  }
`;

export const DELETE_EMPLOYEE = gql`
  mutation DELETE_EMPLOYEE($id: uuid!) {
    delete_employees(where: { id: { _eq: $id } }) {
      returning {
        id
      }
    }
  }
`;

export const QUERY_USERS_BY_EMAIL = gql`
  query QUERY_USERS_BY_EMAIL($email: String!) {
    users(where: { email: { _ilike: $email } }) {
      email
    }
  }
`;

export const INSERT_VACATION = gql`
  mutation INSERT_VACATION($vacation: employee_vacation_insert_input!) {
    insert_employee_vacation_one(object: $vacation) {
      id
      start
      end
    }
  }
`;

export const DELETE_VACATION = gql`
  mutation DELETE_VACATION($id: uuid!) {
    delete_employee_vacation_by_pk(id: $id) {
      id
    }
  }
`;
