import httpClient from '@/api/httpClient';
import { API_URLS } from '@/api/URLs';
import { AxiosResponse } from 'axios';

interface ConfirmBody {
  status: boolean;
  data: {
    detail: string;
    token: {
      access: string;
      refresh: string;
    };
  };
}

export interface CompanyInfo {
  id: number;
  inn: string;
  registration_authority: string;
  registration_date: string;
  registration_number: string;
  name: string;
  short_name: string;
  opf_code: string;
  opf_name: string;
  oked_code: string;
  vat_number: string;
  oked_name: string;
  area: string;
  region: string;
  soogu_code: string;
  soogu_name: string;
  small_businesses: string;
  activity_state: number;
  statutory_fund: string;

  activity_state_detail: ActivityStateDetail;
  business_type_detail: BusinessTypeDetail;

  director: string;
  email: string;
  village_code: string;
  email_status: number;
  phones: string[];

  soato_code: string;
  soato_name: string;
  address: string;

  relevance_date: string;
  court_relevance_date: string | null;
  deal_relevance_date: string | null;

  tax_mode: number;
  trust: string;
  score: number;

  itpark: number;
  leasing_count_all: number | null;
  leasing_count_not_finish: number | null;
  leasing_sum: number | null;
  leasing_is_delay: number | null;
  leasing_debt: number | null;
  leasing_is_partner: number | null;
  leasing_relevance_date: string | null;

  is_bankrupt: number;
  is_abuse_vat: number;
  is_large_taxpayer: number;

  vendor_rating: number | null;
  developer_rating: number | null;

  dishonest_executor: DishonestExecutor;
  village_detail: VillageDetail;
  company_billing_address: BillingAddress;

  actual_date: string;
  kfs: Kfs;

  uuid: string;

  connections: Connections;
  courts: Courts;

  director_uuid: string;
  founders: Founder[];

  deals: Deals;
  licenses: Licenses;

  leasing_guarantor_pinfl: string | null;
  leasing_guarantor_inn: string | null;

  buildings: Buildings;
  cadastres: Cadastres;

  liquidation_date: string | null;
  liquidation_reason: string | null;

  is_suppliers: number;
}
export interface ActivityStateDetail {
  id: number;
  group: string;
  name: string;
  name_en: string;
  name_uz: string;
  name_uz_kir: string;
}

export interface BusinessTypeDetail {
  id: number;
  external_id: number;
  name: string;
  name_uz: string;
  name_en: string;
}

export interface DishonestExecutor {
  is_dishonest_executor: number;
  delete_date: string | null;
}

export interface VillageDetail {
  code: number;
  name: string;
}

export interface BillingAddress {
  country_code: number;
  region_code: number;
  region_name: string;
  region_name_en: string;
  region_name_uz: string;

  district_code: number;
  district_name: string;
  district_name_en: string;
  district_name_uz: string;

  sector_code: number;
  street_name: string;
  house: string | null;
  flat: string | null;
  postcode: string;
}

export interface Kfs {
  code: number;
  name: string;
  name_ru: string;
  name_uz_cyr: string;
  name_uz_lat: string;
}

export interface Connections {
  director: number;
  founders: number;
  entrepreneur: number;
  all: number;
}

export interface Courts {
  total: number;
  current: number;
  completed: number;
}

export interface Founder {
  name: string;
  percentage: number;
  is_individual: number;
  person_type: string;
  id: number | null;
  founder_uuid: string;
}

export interface Deals {
  customer: DealSide;
  provider: DealSide;
  actual_date: string;
}

export interface DealSide {
  rows: any[];
  total: number;
  summ: number | null;
}

export interface Licenses {
  total: number;
  relevance_date: string;
  actual_date: string;
}

export interface Buildings {
  total: number;
}

export interface Cadastres {
  total: number;
  relevance_date: string | null;
}
export interface GetInfoResponse {
  status: boolean;
  data: CompanyInfo | IndividualInfo;
}

export interface IndividualInfo {
  uuid: string,
  id: number,
  lastname: string,
  firstname: string,
  middlename: string,
  registered_at: string,
  unregistered_at: null | string,
  activities: {
    code: number,
    name_en: string,
    name_uz: string,
    name_ru: string
  }[]

}

export interface GetDirectorInfoResponse {
  status: boolean,
  data: {
    entity: {
      name: {
        rows: {
          id: number,
          inn: string,
          name: string,
          director: string,
          email: string,
          phones: string[],
          founders: {
            name: string,
            percentage: number,
            is_individual: number,
            person_type: string,
            id: number | null,
            founder_uuid: string
          }[],
          activity_state: number,
          registration_date: string,
          oked_code: string,
          oked_name: string,
          statutory_fund: string,
          address: string,
          variant: null
        }[],
        total: number
      },
      inn: {
        rows: [],
        total: 0
      },
      director: {
        rows: [],
        total: 0
      },
      founder: {
        rows: {
          id: number,
          inn: string,
          name: string,
          director: string,
          email: string,
          phones: string[],
          founders:
          {
            name: string,
            percentage: number,
            is_individual: number,
            person_type: string,
            id: number | null,
            founder_uuid: string
          }[],
          activity_state: number,
          registration_date: string,
          oked_code: string,
          oked_name: string,
          statutory_fund: string,
          address: string,
          variant: null
        }[],
        total: number
      },
      email: {
        rows: [],
        total: number
      },
      phone: {
        rows: [],
        total: number
      }
    },
    entrepreneur: {
      rows:
      {
        id: number,
        pinfl: string,
        entrepreneur: string,
        email: string,
        phone: string,
        registration_date: string
      }[],
      total: number
    },
    trademark: {
      rows: [],
      total: number
    }
  }
}

export const auth_api = {
  async login(body: { phone: string }) {
    const res = await httpClient.post(API_URLS.LOGIN, body);
    return res;
  },

  async verify_otp(body: { code: string; phone: string }): Promise<AxiosResponse<ConfirmBody>> {
    const res = await httpClient.post(API_URLS.LoginConfirm, body);
    return res;
  },
  async resend_otp(body: { phone: string }) {
    const res = await httpClient.post(API_URLS.ResendOTP, body);
    return res;
  },

  async get_info(body: { value: string, type: string, passport_series?: string, passport_number?: string }): Promise<AxiosResponse<GetInfoResponse>> {
    const res = await httpClient.post(API_URLS.Info, body);
    return res;
  },

  async get_director_info(body: { value: string }): Promise<AxiosResponse<GetDirectorInfoResponse>> {
    const res = await httpClient.post(API_URLS.Get_Director_Info, body);
    return res;
  },

  async register(body: {
    phone: string;
    stir: string;
    person_type: string;
    referal: string;
    activate_types: number[];
    director_full_name: string;
    first_name: string;
    last_name: string;
  }) {
    const res = await httpClient.post(API_URLS.Register, body);
    return res;
  },

  async register_confirm(body: { phone: string; code: string }) {
    const res = await httpClient.post(API_URLS.Register_Confirm, body);
    return res;
  },

  async register_resend(body: { phone: string }) {
    const res = await httpClient.post(API_URLS.Register_Resend, body);
    return res;
  },
};
