// ─── Entities ──────────────────────────────────────────────────────────────
export interface Country {
  id: number;
  name: string;
  code: string;
  flag_url: string;
}

export interface Standard {
  id: number;
  name: string;
  title: string;
}

export interface CompanyDocument {
  id: number;
  name: string;
  url: string;
}

export interface Company {
  id: number;
  company_code: string;
  name: string;
  normalized_name?: string;            // 🆕 from backend fix
  trade_license_no?: string | null;    // 🆕 from backend fix
  address: string;
  city: string;
  contact_person: string;
  designation: string;
  email: string;
  mobile: string;
  telephone: string;
  fax: string;
  validity: string;
  certification_body: string;
  client_group: string;
  accreditation: string;
  scope_of_work: string;
  reference_number: string;
  documents: CompanyDocument[];
  country: Country;
  standards: Standard[];
  created_at: string;
  updated_at: string;
  // 🆕 ISSUE 1 FIX — 5 columns that exist in DB, now exposed to frontend
  created_by_id?: number | null;
  legacy_source?: 'QRS' | 'TQS' | null;
  legacy_ref_id?: number | null;
  client_status?: 'LEAD' | 'PROSPECT' | 'ACTIVE' | 'INACTIVE';
  nc_stamp_img?: string | null;
}

// ─── DTOs ──────────────────────────────────────────────────────────────────
export interface CreateCompanyDto {
  company_code?: string;
  name: string;
  address: string;
  city: string;
  contact_person: string;
  designation?: string;
  email: string;
  mobile: string;
  telephone?: string;
  fax?: string;
  validity?: string;
  certification_body?: string;
  client_group?: string;
  accreditation?: string;
  scope_of_work?: string;
  reference_number?: string;
  country_id?: number;
  standard_ids?: number[];
}

export type UpdateCompanyDto = Partial<CreateCompanyDto>;

// ─── Pagination meta (exported for use in tables) ──────────────────────────
export interface PaginationMeta {
  total: number;
  page: number;
  limit: number;
  totalPages: number;
  hasNextPage: boolean;
  hasPrevPage: boolean;
}

// ─── Paginated response ─────────────────────────────────────────────────────
export interface PaginatedCompaniesResponse {
  data: Company[];
  meta: PaginationMeta;
}

// ─── Table row (used in CompanyTable / CompanyRow / DynamicCompanyRow) ───────
export interface CompanyRow {
  sno: number;
  id: number;
  company_code: string;
  name: string;
  address: string;
  city: string;
  contact_person: string;
  designation: string;
  email: string;
  mobile: string;
  telephone: string;
  fax: string;
  validity: string;
  certification_body: string;
  client_group: string;
  accreditation: string;
  scope_of_work: string;
  reference_number: string;
  standards: Standard[];
  country: Country;
  created_at: string;
  updated_at: string;
  // 🆕 new fields
  client_status?: string;
  trade_license_no?: string | null;
  [key: string]: any;
}

// ═══════════════════════════════════════════════════════════════════════════
// 🆕 ISSUE 5 & 6 — Portal access types
// ═══════════════════════════════════════════════════════════════════════════

export type PortalUserStatus = 'ACTIVE' | 'INVITED' | 'DISABLED';

export interface PortalUser {
  company_id: number;
  user_id: number;
  role: string;
  status: PortalUserStatus;
  invite_email: string | null;
  granted_by_id: number | null;
  granted_by?: { id: number; email: string; firstName?: string; lastName?: string } | null;
  disabled_at: string | null;
  disabled_by_id: number | null;
  disabled_by?: { id: number; email: string; firstName?: string; lastName?: string } | null;
  last_login_at: string | null;
  created_at: string;
  user?: { id: number; email: string; firstName?: string; lastName?: string } | null;
}