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

export const QUERY_SEARCH_OWNER_BY_IDX = gql`
  query QUERY_SEARCH_OWNER_BY_IDX($search: String!) {
    search_owners(args: { search: $search }, limit: 100) {
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_ph1
      ownr_ph2
      ownr_ph1_ty
      ownr_ph2_ty
      ownr_addr1
      ownr_addr2
      ownr_city
      ownr_ctry
      ownr_ea
      ownr_st
      ownr_zip
      id
      note
    }
  }
`;

export const SEARCH_OWNERS_BY_ID_FOR_AUTOCOMPLETE = gql`
  query SEARCH_OWNERS_BY_ID_FOR_AUTOCOMPLETE($id: uuid!) {
    owners_by_pk(id: $id) {
      id
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_addr1
    }
  }
`;
export const SEARCH_OWNERS_FOR_AUTOCOMPLETE = gql`
  query SEARCH_OWNERS_FOR_AUTOCOMPLETE($search: String) {
    search_owners(args: { search: $search }, limit: 25, order_by: { ownr_ln: desc_nulls_last }) {
      id
      ownr_fn
      ownr_ln
      ownr_co_nm
      ownr_addr1
    }
  }
`;

export const QUERY_OWNER_BY_ID = gql`
  query QUERY_OWNER_BY_ID($id: uuid!) {
    owners_by_pk(id: $id) {
      id
      accountingid
      ownr_addr1
      ownr_addr2
      ownr_co_nm
      ownr_city
      ownr_ctry
      ownr_ea
      ownr_fn
      ownr_ph1
      ownr_ph1_ty
      ownr_ln
      ownr_ph2
      ownr_ph2_ty
      ownr_st
      ownr_title
      ownr_zip
      preferred_contact
      note
      tax_number
      jobs(order_by: { date_open: desc }) {
        id
        actual_completion
        ro_number
        clm_no
        status
        clm_total
        v_model_yr
        v_model_desc
        v_make_desc
        vehicleid
      }
    }
  }
`;

export const UPDATE_OWNER = gql`
  mutation UPDATE_OWNER($ownerId: uuid!, $owner: owners_set_input!) {
    update_owners(where: { id: { _eq: $ownerId } }, _set: $owner) {
      returning {
        id
      }
    }
  }
`;

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

export const QUERY_ALL_OWNERS = gql`
  query QUERY_ALL_OWNERS {
    owners {
      id
      created_at
      ownr_addr1
      ownr_addr2
      ownr_co_nm
      ownr_city
      ownr_ctry
      ownr_ea
      ownr_fn
      ownr_ph1
      ownr_ph1_ty
      ownr_ln
      ownr_ph2
      ownr_ph2_ty
      ownr_st
      ownr_title
      ownr_zip
      preferred_contact
      updated_at
    }
  }
`;

export const QUERY_ALL_OWNERS_PAGINATED = gql`
  query QUERY_ALL_OWNERS_PAGINATED($search: String, $offset: Int, $limit: Int, $order: [owners_order_by!]!) {
    search_owners(args: { search: $search }, offset: $offset, limit: $limit, order_by: $order) {
      id
      created_at
      ownr_addr1
      ownr_addr2
      ownr_co_nm
      ownr_city
      ownr_ctry
      ownr_ea
      ownr_fn
      ownr_ph1
      ownr_ph1_ty
      ownr_ln
      ownr_ph2
      ownr_ph2_ty
      ownr_st
      ownr_title
      ownr_zip
      preferred_contact
      updated_at
    }
    search_owners_aggregate(args: { search: $search }) {
      aggregate {
        count(distinct: true)
      }
    }
  }
`;

export const QUERY_OWNER_FOR_JOB_CREATION = gql`
  query QUERY_OWNER_FOR_JOB_CREATION($id: uuid!) {
    owners_by_pk(id: $id) {
      id
      ownr_addr1
      ownr_addr2
      ownr_co_nm
      ownr_city
      ownr_ctry
      ownr_ea
      ownr_fn
      ownr_ph1
      ownr_ph1_ty
      ownr_ln
      ownr_ph2
      ownr_ph2_ty
      ownr_st
      ownr_title
      ownr_zip
    }
  }
`;
