import { UUID } from "crypto";
import { parse, TypedQueryDocumentNode } from "graphql";
import { gql } from "graphql-request";
import { AvailableJobSchema } from "../decoder/decoder";

// Define types for the query result and variables
export interface ActiveBodyshopQueryResult {
  bodyshops: Array<{
    id: string;
    shopname: string;
    region_config: string;
    convenient_company: string;
  }>;
}

// No variables needed for this query
export const QUERY_ACTIVE_BODYSHOP_TYPED: TypedQueryDocumentNode<
  ActiveBodyshopQueryResult,
  Record<never, never>
> = parse(gql`
  query QUERY_ACTIVE_BODYSHOP {
    bodyshops(where: { associations: { active: { _eq: true } } }) {
      id
      shopname
      region_config
      convenient_company
    }
  }
`) as TypedQueryDocumentNode<ActiveBodyshopQueryResult, Record<never, never>>;

export interface MasterdataQueryResult {
  masterdata: Array<{
    value: string;
    key: string;
  }>;
}

interface MasterdataQueryVariables {
  key: string;
}

export const QUERY_MASTERDATA_TYPED: TypedQueryDocumentNode<
  MasterdataQueryResult,
  MasterdataQueryVariables
> = parse(gql`
  query QUERY_MASTERDATA($key: String!) {
    masterdata(where: { key: { _eq: $key } }) {
      value
      key
    }
  }
`) as TypedQueryDocumentNode<MasterdataQueryResult, MasterdataQueryVariables>;

export interface VehicleQueryResult {
  vehicles: Array<{
    id: UUID;
  }>;
}

interface VehicleQueryVariables {
  vin: string;
}

export const QUERY_VEHICLE_BY_VIN_TYPED: TypedQueryDocumentNode<
  VehicleQueryResult,
  VehicleQueryVariables
> = parse(gql`
  query QUERY_VEHICLE_BY_VIN($vin: String!) {
    vehicles(where: { v_vin: { _eq: $vin } }) {
      id
    }
  }
`) as TypedQueryDocumentNode<VehicleQueryResult, VehicleQueryVariables>;

export interface QueryJobByClmNoResult {
  jobs: Array<{
    id: UUID;
  }>;
}

export interface QueryJobByClmNoVariables {
  clm_no: string;
}

export const QUERY_JOB_BY_CLM_NO_TYPED: TypedQueryDocumentNode<
  QueryJobByClmNoResult,
  QueryJobByClmNoVariables
> = parse(gql`
  query QUERY_JOB_BY_CLM_NO($clm_no: String!) {
    jobs(where: { clm_no: { _eq: $clm_no } }) {
      id
    }
  }
`) as TypedQueryDocumentNode<QueryJobByClmNoResult, QueryJobByClmNoVariables>;

export interface InsertAvailableJobResult {
  returning: Array<{
    id: UUID;
  }>;
}

export interface InsertAvailableJobVariables {
  jobInput: Array<AvailableJobSchema>;
}

export const INSERT_AVAILABLE_JOB_TYPED: TypedQueryDocumentNode<
  InsertAvailableJobResult,
  InsertAvailableJobVariables
> = parse(gql`
  mutation INSERT_AVAILABLE_JOB($jobInput: [available_jobs_insert_input!]!) {
    insert_available_jobs(
      objects: $jobInput
      on_conflict: {
        constraint: available_jobs_clm_no_bodyshopid_key
        update_columns: [
          clm_amt
          cieca_id
          est_data
          issupplement
          ownr_name
          source_system
          supplement_number
          vehicle_info
        ]
      }
    ) {
      returning {
        id
      }
    }
  }
`) as TypedQueryDocumentNode<
  InsertAvailableJobResult,
  InsertAvailableJobVariables
>;

// Add PpgData Query
export interface PpgDataQueryResult {
  bodyshops_by_pk: {
    id: string;
    shopname: string;
    imexshopid: string;
  } | null;
  jobs: Array<{
    id: string;
    ro_number: string;
    status: string;
    ownr_fn: string;
    ownr_ln: string;
    ownr_co_nm: string;
    v_vin: string;
    v_model_yr: string;
    v_make_desc: string;
    v_model_desc: string;
    v_color: string;
    plate_no: string;
    ins_co_nm: string;
    est_ct_fn: string;
    est_ct_ln: string;
    rate_mapa: number;
    rate_lab: number;
    job_totals: {
      rates?: {
        mapa?: {
          total?: {
            amount?: number;
          };
        };
      };
      totals?: {
        subtotal?: {
          amount?: number;
        };
      };
    };
    vehicle: {
      v_paint_codes: {
        paint_cd1?: string;
      };
    };
    labhrs: {
      aggregate: {
        sum: {
          mod_lb_hrs: number;
        };
      };
    };
    larhrs: {
      aggregate: {
        sum: {
          mod_lb_hrs: number;
        };
      };
    };
  }>;
}

export interface PpgDataQueryVariables {
  today: string;
  todayplus5: string;
  shopid: string;
}

export const PPG_DATA_QUERY_TYPED: TypedQueryDocumentNode<
  PpgDataQueryResult,
  PpgDataQueryVariables
> = parse(gql`
  query PpgData(
    $today: timestamptz!
    $todayplus5: timestamptz!
    $shopid: uuid!
  ) {
    bodyshops_by_pk(id: $shopid) {
      id
      shopname
      imexshopid
    }
    jobs(
      where: {
        _or: [
          {
            _and: [
              { scheduled_in: { _lte: $todayplus5 } }
              { scheduled_in: { _gte: $today } }
            ]
          }
          { inproduction: { _eq: true } }
        ]
      }
    ) {
      id
      ro_number
      status
      ownr_fn
      ownr_ln
      ownr_co_nm
      v_vin
      v_model_yr
      v_make_desc
      v_model_desc
      v_color
      plate_no
      ins_co_nm
      est_ct_fn
      est_ct_ln
      rate_mapa
      rate_lab
      job_totals
      vehicle {
        v_paint_codes
      }
      labhrs: joblines_aggregate(
        where: { mod_lbr_ty: { _neq: "LAR" }, removed: { _eq: false } }
      ) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
      larhrs: joblines_aggregate(
        where: { mod_lbr_ty: { _eq: "LAR" }, removed: { _eq: false } }
      ) {
        aggregate {
          sum {
            mod_lb_hrs
          }
        }
      }
    }
  }
`) as TypedQueryDocumentNode<PpgDataQueryResult, PpgDataQueryVariables>;
