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

// Transform the string query into a TypedQueryDocumentNode
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
>;
