import type { BackendPaginatedData } from "@/shared/types";

export interface AccessStatus {
  /** "1" Enabled · "2" Disabled · "0" Pending */
  value: string;
  label: string;
  enabled: boolean;
  /** Badge color key: "success" | "failure" | "warning" */
  color: string;
}

export interface Company {
  id: number;
  company_name: string;
  siret: string;
  first_name: string;
  last_name: string;
  full_name: string;
  email: string;
  phone: string;
  vehicles_count: number;
  sites_count: number;
  employees_count: number;
  subscription_plan: string | null;
  access_status: AccessStatus;
  /** Flagged as test/demo account — excluded from Customer Health statistics. */
  is_test_account: boolean;
  /** SCRUM-315 — inter-company refacturation feature granted to this company's group. */
  refacturation_feature_granted: boolean;
  /** Early access to Donna (the fleet AI agents) before the public release. */
  ai_agents_enabled: boolean;
  email_verified: boolean;
  created_at: string;
  profile_picture: string | null;
}

export interface CompanyDetail {
  id: number;
  company_name: string;
  first_name: string;
  last_name: string;
  full_name: string;
  email: string;
  phone: string;
  email_verified: boolean;
  email_verified_at: string | null;
  profile_picture: string | null;
  preferred_language: string | null;
  company: {
    id: number;
    name: string;
    siret: string;
    siren: string;
    email: string;
    phone: string;
    address: string;
    city: string;
    postal_code: string;
    website: string | null;
    private_mode: boolean;
    subscription_plan: string | null;
    subscription_plan_id: number | null;
  } | null;
  counts: {
    vehicles: number;
    sites: number;
    employees: number;
    issues: number;
  };
  subscription: {
    id: number;
    status: string;
    is_trial: boolean;
    trial_ends_at: string | null;
    starts_at: string | null;
    ends_at: string | null;
  } | null;
  created_at: string;
  updated_at: string;
}

export interface CompanyStats {
  total: number;
  with_vehicles: number;
  with_subscription: number;
  new_last_30_days: number;
}

/** Compact company returned by POST /companies/{id}/toggle-status. */
export interface CompanyAccessToggleResult {
  id: number;
  access_status: AccessStatus;
}

/** Returned by POST /companies/{id}/toggle-test. */
export interface CompanyTestToggleResult {
  id: number;
  is_test_account: boolean;
}

/** Returned by POST /companies/{id}/toggle-ai-agents. */
export interface CompanyAiAgentsToggleResult {
  id: number;
  ai_agents_enabled: boolean;
}

/** SCRUM-315 — returned by POST /companies/{id}/toggle-refacturation. */
export interface CompanyRefacturationToggleResult {
  id: number;
  holding_company_id: number;
  feature_granted: boolean;
  enabled: boolean;
}

export interface CreateCompanyPayload {
  name: string;
  siret: string;
  first_name: string;
  last_name: string;
  email: string;
  phone: string;
  password: string;
}

export interface CompanyOption {
  value: number;
  label: string;
}

export interface CompanyLookupResult {
  name: string;
  siren: string;
  siret: string | null;
  address: string;
  postal_code: string;
  city: string;
}

export interface UpdateCompanyPayload {
  name?: string;
  siret?: string;
  first_name?: string;
  last_name?: string;
  email?: string;
  phone?: string;
}

/** A free account told, closed or restored for long inactivity (freemium spec 6.4). */
export interface DormantAccountRow {
  /** companies.id */
  company_id: number;
  /** The company account (users.id). */
  account_id: number;
  company_name: string;
  owner_name: string;
  owner_email: string | null;
  status: "notified" | "closed" | "restored";
  last_activity_at: string | null;
  notified_at: string | null;
  close_on: string | null;
  closed_at: string | null;
  restored_at: string | null;
  restored_by: string | null;
}

export interface DormantAccountsData extends BackendPaginatedData<DormantAccountRow> {
  counts: { notified: number; closed: number; restored: number };
}
