import GraphQLService from './GraphQLService';

export interface UsAdress{
  address: string;
  city_id: number;
  state_id: number;
  country_id: number;
  primary_email:string
}
export interface UserAddressCreateResponse {
  id: number;
  address: string;
  city_id: number;
  state_id: number;
  country_id: number;
}
export interface AddressCreateOPData {
    UserAddressCreate?:UserAddressCreateResponse[]
}

export interface UsAdressData{
  id? : number
  address: string;
  city_id: number;
  state_id: number;
  country_id: number; 
}

interface SnapshotData {
  message : string
  result : string
  imp : string
  mismatch : string
  notFound : string
  conflict : string
  success : string
  error : string
  alert : string
  warning : string
  notice : string
  info : string
}

export interface AddressUpdateOPData {
    UserAddressUpdate?:{
    snapshot: SnapshotData
    affected : number
    affectedRows ?: UsAdressData[]
  }
}

export interface AddressDeleteOPData {
    UserAddressDelete?:{
    snapshot: SnapshotData
    affected : number
  }
}
class CustomerAddressService extends GraphQLService {
    constructor() {
      super();
    }

    createCustomerAddress(CustomerAddrslINData:UsAdress,singupID:number):Promise<AddressCreateOPData |null | undefined>{
      const Query =`mutation Mutation($input: [UserAddressCreateInput!]!) {
                    UserAddressCreate(input: $input) {
                        id
                        address
                    }
                    }`;
      const variables = {
        input:[{
          "u_id": parseInt(`${singupID}`),
          "country_id": parseInt(`${CustomerAddrslINData.country_id}`),
          "city_id": parseInt(`${CustomerAddrslINData.city_id}`),
          "address": CustomerAddrslINData.address,
          "state_id": parseInt(`${CustomerAddrslINData.state_id}`),
          "email": CustomerAddrslINData.primary_email
        }]
      };
  
      return this.mutate<AddressCreateOPData>(Query, variables); 
    }

    updateCustomerAddress(customerAddINData: UsAdressData,userID:number):Promise<AddressUpdateOPData |null | undefined>{
      const Query =`mutation UserAddressUpdate($input: UserAddressUpdateInput!) {
                    UserAddressUpdate(input: $input) {
                         snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                        affected
                        affectedRows {
                        id
                        u_id
                        address
                        }
                    }
                    }`; 
        const variables = {
                "input": {
                    "sets": {
                    "address": customerAddINData.address,
                    "country_id": parseInt(`${customerAddINData.country_id}`),
                    "city_id": parseInt(`${customerAddINData.city_id}`),
                    "state_id": parseInt(`${customerAddINData.state_id}`),
                    },
                    "where": [
                    {
                        "u_id": {
                        "equal": `${userID}`
                        }
                    }
                    ]
                }
            };
  
      return this.mutate<AddressUpdateOPData>(Query, variables); 
    }
    
    deleteCustomerAddress(Id: number):Promise<AddressDeleteOPData |null | undefined>{
      const Query =`mutation UserAddressDelete($input: UserAddressDeleteInput!) {
                    UserAddressDelete(input: $input) {
                        snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                        affected
                    }
                    }`;
            const variables = {
              input:{
                "where": [
                  {
                    "u_id": {
                      "equal": `${Id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<AddressDeleteOPData>(Query, variables); 
    }
    
}

const CustomerAddressServiceInstance = new CustomerAddressService();
export default CustomerAddressServiceInstance;