import GraphQLService from './GraphQLService';
import { CUSTOMER_AR_ID } from '@/constant/StaticData';

// Main Interface for UserFind Response
interface UserFindResponse {
    rows: UserRow[];
    total: number;
    pages: number;

}

// Row Structure
interface UserRow {
  id: string | number;
  username: string;
  primary_email: string;
  primary_mobile: string;
  primary_mobile_cc: string;
  recovery_mobile_cc?: string; 
  signup_source: string;
  fname:string;
  lname:string;
  fr_user_addresses: UserAddress[];
  fr_user_authorisations:[{
    id:number
    au_id:number
    ar_id:number
  }]
}


// Address Structure
interface UserAddress {
  id: string;
  address: string;
  country_id: number ;
  state_id: number;
  city_id: string  | number;
  fr_country: frCountry | null;
  fr_state: frState | null;
  fr_city: frCity | null;
}

// Country Structure
interface frCountry {
  id: string;
  name: string;
}

// State Structure
interface frState {
  id: string;
  name: string;
}

// City Structure
interface frCity {
  id: string;
  name: string;
  state_id: string;
}

interface snapshot {
    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 CountryFindResponse {
    total: number;
    remain: number;
    pages: number;
    take: number;
    rows: CountryRow[];  
 
}

export interface CountryRow {
  id: number;
  name: string;
  numeric_code: string;
  phone_code: string;
}


export interface CountryFindOneByIdResponse {
    fr_states: State[]; 
}

export interface State {
  id: number;
  name: string;
  country_id: string ;
  fr_cities: City[]; 
}

export interface City {
  id: number;
  name: string;
  state_id: number;
}
export interface customerUpdateData{
  id?:number
  username:string
  primary_mobile:number
  primary_mobile_cc:string
  primary_email:string
}

export interface CustomerData{
  id?:number
  username:string
  primary_email:string
  primary_mobile:number
  primary_mobile_cc:string
  signup_source:string
  fname:string
  lname:string
  ar_id?: number | string
  fr_user_addresses?: UserAddress[];
}
export interface CustomerOPData {
  UserFind: UserFindResponse;
}

 export interface CustomerIdInData {
  UserFindOneById: UserRow;
} 
 
export interface CountryOPData{
  CountryFind:CountryFindResponse;
}

export interface CityOPData{
  CountryFindOneById:CountryFindOneByIdResponse ;
}
export interface CustomerUpdateOPData {
  UserUpdate?:{
    snapshot?: snapshot
    affected : number
    affectedRows ?: customerUpdateData[]
  }
}

export interface CustomerDeleteOPData {
  UserDelete?:{
    snapshot?: snapshot
    affected : number
  }
}

export interface AuthenticationDeleteOPData {
  UserAuthenticationDelete?:{
    snapshot?: snapshot
    affected : number
  }
}

export interface AuthenthorizationDeleteOPData {
  UserAuthorisationDelete?:{
    snapshot?: snapshot
    affected : number
  }
}

export interface CustomerUpsertOPData {
 }

export interface CustomerSignOPData{
  AppSignup?:{
    id?:number
    username:string
    primary_email:string
    primary_mobile:number
    primary_mobile_cc:string
    recovery_email:string
    signup_source:string
    fr_user_addresses?: UserAddress[];
  }
} 

class CustomerService extends GraphQLService {
    constructor() {
      super();
    }

    getCustomer(pageSize:number, start:number, quickSearch:string,sortorder:string):Promise<CustomerOPData |null | undefined>{

      const filter = {
        "where": [
              {
                "fr_user_authorisations": [
                  {
                    "ar_id": {
                      "equal":` ${CUSTOMER_AR_ID}`
                    }
                  }
                ],
                ...(quickSearch) ? {
                  "fname": {
                    "like": `%${quickSearch}%`
                  }
                } : null
              },
              {
                "fr_user_authorisations": [
                  {
                    "ar_id": {
                      "equal":` ${CUSTOMER_AR_ID}`
                    }
                  }
                ],
              ...(quickSearch) ? {
                "lname": {
                  "like": `%${quickSearch}%`
                }
              } : null
            },
            ],
            "order": {
              "id": sortorder
            },
            "skip": start,
            "take": pageSize
      };


      const Query =`query UserFind($filter: UserFindInput!) {
                    UserFind(filter: $filter) {
                      rows {
                        id
                        username
                        primary_email
                        primary_mobile
                        primary_mobile_cc
                        recovery_mobile_cc
                        signup_source
                        fname
                        lname
                        fr_user_addresses {
                          id
                          address
                          country_id
                          state_id
                          city_id
                          fr_country {
                            id
                            name
                          }
                          fr_state {
                            id
                            name
                          }
                          fr_city {
                            id
                            name
                            state_id
                          }
                        }
                        fr_user_authorisations {
                          id
                          au_id
                          ar_id
                        }
                      }
                      total
                      pages
                    }
                  }`;
        const variables = {filter};
  
      return this.execute(Query, variables); 
    }

    getCountry():Promise<CountryOPData |null | undefined>{
      const Query =`query Query($filter: CountryFindInput!) {
                      CountryFind(filter: $filter) {
                        total
                        remain
                        pages
                        take
                        rows {
                          id
                          name
                          numeric_code
                          phone_code
                        }
                      }
                    }`;
      const variables = {
          "filter": {
            "order": {
              "id": "ASC"
            }
          }
      };
  
      return this.execute(Query, variables); 
    }

    getCitybyID(Id: number):Promise<CityOPData |null | undefined>{
      const Query =`query Rows($input: CountryFindOneByIdInput!) {
                      CountryFindOneById(input: $input) {
                        fr_states {
                          id
                          name
                          country_id
                          fr_cities {
                            id
                            name
                            state_id
                          }
                          
                        }
                      }
                    }`;
      const variables = {
          "input": {
            "id": Id
          }
      };
  
      return this.execute(Query, variables); 
    }

    getCustomerById(Id: number):Promise<CustomerIdInData |null | undefined>{
      const Query=`query UserFindOneById($input: UserFindOneByIdInput!) {
                    UserFindOneById(input: $input) {
                      id
                      username
                      primary_email
                      primary_mobile
                      primary_mobile_cc
                      signup_source
                      fname
                      lname
                     
                      fr_user_addresses {
                        u_id
                        id
                        address
                        country_id
                        state_id
                        city_id
                        fr_country {
                          id
                          name
                        }
                        fr_state {
                          id
                          name
                        }
                        fr_city {
                          id
                          name
                          state_id
                        }
                      }
                      fr_user_authorisations {
                        id
                        au_id
                        ar_id
                      }
                    }
                  } `
    
      const variables = {
          "input": {
            "id": Id
          }
      };
  
      return this.execute(Query, variables); 
    }

    createCustomerSignUp(CustomerINData:CustomerData):Promise<CustomerSignOPData |null | undefined>{
      
      const Query =`mutation  ($input: AppSignupInput!) {
                      AppSignup(input: $input) {
                        id
                        username
                        primary_email
                        primary_mobile
                        primary_mobile_cc
                        recovery_email
                        signup_source
                        
                      }
                    }`;
      const variables = {
        "input": {
          "signup_source": CustomerINData.signup_source,
          "ar_id":CUSTOMER_AR_ID,
          "primary_email": CustomerINData.primary_email,
          "primary_mobile": CustomerINData.primary_mobile,
          "primary_mobile_cc": "1",
         
        }
      };
      
  
      return this.mutate<CustomerSignOPData>(Query, variables); 
    }
   
    updateCustomer(CustomerINData: CustomerData,Id:number):Promise<CustomerUpdateOPData |null | undefined>{
      const Query =`mutation UserUpdate($input: UserUpdateInput!) {
                    UserUpdate(input: $input) {
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                      affectedRows {
                        id
                        username
                        primary_mobile
                        primary_mobile_cc
                        primary_email
                      }
                    }
                  }`;
      const variables = {
                "input": {
                  "sets": {
                    "primary_email": CustomerINData.primary_email,
                    "primary_mobile":CustomerINData.primary_mobile,
                    "primary_mobile_cc": "1",
                    "fname":CustomerINData.fname,
                    "lname":CustomerINData.lname

                  },
                  "where": [
                    {
                      "id": {
                        "equal": `${Id}`
                      }
                    }
                  ]
                }
            };
        
      return this.mutate<CustomerUpdateOPData>(Query, variables); 
    }

    upsertCustomer(CustomerINData: CustomerData):Promise<CustomerUpsertOPData |null | undefined>{
      const Query =``;
            const variables = {
              input:[CustomerINData]
            };
  
      return this.mutate<CustomerUpsertOPData>(Query, variables); 
    }

    deleteCustomer(Id: number):Promise<CustomerDeleteOPData |null | undefined>{
      const Query =`mutation UserDelete($input: UserDeleteInput!) {
                    UserDelete(input: $input) {
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                    }
                  }`;
            const variables = {
              input:{
                "where": [
                  {
                    "id": {
                      "equal": `${Id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<CustomerDeleteOPData>(Query, variables); 
    }

    deleteCustomerAuthentication(Id: number):Promise<AuthenticationDeleteOPData |null | undefined>{
      const Query =`mutation UserAuthenticationDelete($input: UserAuthenticationDeleteInput!) {
                    UserAuthenticationDelete(input: $input) {
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                    }
                  }`;
            const variables = {
              input:{
                "where": [
                  {
                    "id": {
                      "equal": `${Id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<AuthenticationDeleteOPData>(Query, variables); 
    }
    
    deleteCustomerAuthorization(Id: number):Promise<AuthenthorizationDeleteOPData |null | undefined>{
      const Query =`mutation UserAuthorisationDelete($input: UserAuthorisationDeleteInput!) {
                    UserAuthorisationDelete(input: $input) {
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                    }
                  }`;
            const variables = {
              input:{
                "where": [
                  {
                    "id": {
                      "equal": `${Id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<AuthenthorizationDeleteOPData>(Query, variables); 
    }

    sortAscCustomer(sortOrder:string,pageSize:number, start:number):Promise<CustomerOPData |null | undefined>{
      const Query =`query UserFind($filter: UserFindInput!) {
                    UserFind(filter: $filter) {
                      rows {
                        id
                        username
                        primary_email
                        primary_mobile
                        primary_mobile_cc
                        recovery_mobile_cc
                        signup_source
                        fname
                        lname
                        fr_user_addresses {
                          id
                          address
                          country_id
                          state_id
                          city_id
                          fr_country {
                            id
                            name
                          }
                          fr_state {
                            id
                            name
                          }
                          fr_city {
                            id
                            name
                            state_id
                          }
                        }
                        fr_user_authorisations {
                          id
                          au_id
                          ar_id
                        }
                      }
                      total
                      pages
                    }
                  }`;
                  const variables = 
                  {
                    "filter": {
                      "where": [
                        {
                          "fr_user_authorisations": [
                            {
                              "ar_id": {
                                "equal":` ${CUSTOMER_AR_ID}`
                              }
                            }
                          ]
                        }
                      ],
                      "order": {
                        "fname": sortOrder,
                        "lname":sortOrder
                      },
                      "skip": start,
                      "take": pageSize
                    }
                  }

      return this.execute(Query, variables); 
    }
    
}

const customerServiceInstance = new CustomerService();
export default customerServiceInstance;