/**
 * Tipos del módulo CRM — reflejan los DTOs del backend (api/v1/modules/crm).
 */

export type AccountStatus = 'active' | 'inactive';
export type RecordType = 'cliente' | 'prospecto';
export type PersonType = 'fisica' | 'moral';
export type LeadStatus = 'new' | 'contacted' | 'qualified' | 'unqualified' | 'converted';
export type OpportunityStatus = 'open' | 'won' | 'lost';
export type ActivityType = 'call' | 'meeting' | 'task' | 'email' | 'note';
export type ActivityStatus = 'pending' | 'completed' | 'canceled';

// ---------- Clientes / Prospectos ----------
export interface Account {
	id: number;
	name: string;
	trade_name: string | null;
	rfc: string | null;
	curp: string | null;
	record_type: RecordType;
	person_type: string | null;
	industry: string | null;
	industry_other: string | null;
	account_type: string | null;
	status: AccountStatus;
	commercial_classification: string | null;
	preferred_contact_method: string | null;
	preferred_contact_other: string | null;
	language: string | null;
	email: string | null;
	phone: string | null;
	website: string | null;
	commercial_observations: string | null;
	/** Texto libre histórico; lo que vale al timbrar son las claves del SAT de abajo. */
	tax_regime: string | null;
	cfdi_use: string | null;
	tax_regime_id: number | null;
	cfdi_use_id: number | null;
	payment_method: string | null;
	payment_form: string | null;
	currency: string | null;
	credit_limit: number | null;
	credit_days: number | null;
	commercial_terms: string | null;
	patente_aduanal: string | null;
	address: string | null;
	city: string | null;
	state: string | null;
	country: string | null;
	notes: string | null;
	internal_notes: string | null;
	owner_user_id: string | null;
	created_by: string | null;
	updated_by: string | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type AccountInput = Partial<Omit<Account, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at' | 'created_by' | 'updated_by'>> & {
	name: string;
};

// ---------- Proveedores ----------
export interface Supplier {
	id: number;
	name: string;
	trade_name: string | null;
	rfc: string | null;
	curp: string | null;
	person_type: string | null;
	status: AccountStatus;
	classifications: string[];
	classification_other: string | null;
	services_offered: string | null;
	coverage: string | null;
	countries: string[];
	ports: string[];
	airports: string[];
	customs: string[];
	business_hours: string | null;
	quote_currency: string | null;
	avg_response_time: string | null;
	commercial_notes: string | null;
	email: string | null;
	phone: string | null;
	website: string | null;
	tax_regime: string | null;
	payment_method: string | null;
	payment_form: string | null;
	credit_limit: number | null;
	credit_days: number | null;
	commercial_terms: string | null;
	notes: string | null;
	internal_notes: string | null;
	owner_user_id: string | null;
	created_by: string | null;
	updated_by: string | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type SupplierInput = Partial<Omit<Supplier, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at' | 'created_by' | 'updated_by'>> & {
	name: string;
};

// ---------- Contactos ----------
export interface Contact {
	id: number;
	account_id: number | null;
	supplier_id: number | null;
	first_name: string;
	last_name: string | null;
	job_title: string | null;
	department: string | null;
	area: string | null;
	email: string | null;
	phone: string | null;
	extension: string | null;
	mobile: string | null;
	whatsapp: string | null;
	is_primary: boolean;
	receives_quotes: boolean;
	receives_invoices: boolean;
	receives_commercial_info: boolean;
	status: string;
	owner_user_id: string | null;
	notes: string | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type ContactInput = Partial<Omit<Contact, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at'>> & {
	first_name: string;
};

// ---------- Direcciones ----------
export interface Address {
	id: number;
	account_id: number | null;
	supplier_id: number | null;
	address_type: string;
	street: string | null;
	ext_number: string | null;
	int_number: string | null;
	neighborhood: string | null;
	postal_code: string | null;
	city: string | null;
	state: string | null;
	country: string | null;
	reference_notes: string | null;
	is_primary: boolean;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type AddressInput = Partial<Omit<Address, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at'>>;

// ---------- Documentos ----------
export interface Document {
	id: number;
	account_id: number | null;
	supplier_id: number | null;
	doc_type: string;
	name: string;
	file_key: string | null;
	file_url: string | null;
	content_type: string | null;
	size_bytes: number | null;
	uploaded_by: string | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type DocumentInput = Partial<Omit<Document, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at' | 'uploaded_by'>> & {
	doc_type: string;
	name: string;
};

// ---------- Prospectos (leads / funnel) ----------
export interface Lead {
	id: number;
	name: string;
	contact_name: string | null;
	email: string | null;
	phone: string | null;
	company_name: string | null;
	source: string | null;
	preferred_contact_method: string | null;
	status: LeadStatus;
	estimated_value: number | null;
	owner_user_id: string | null;
	converted_account_id: number | null;
	converted_contact_id: number | null;
	converted_opportunity_id: number | null;
	notes: string | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type LeadInput = Partial<Omit<Lead, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at' | 'converted_account_id' | 'converted_contact_id' | 'converted_opportunity_id'>> & {
	name: string;
};

export interface LeadConvertInput {
	create_opportunity?: boolean;
	opportunity_name?: string | null;
	pipeline_id?: number | null;
	stage_id?: number | null;
	amount?: number | null;
}

export interface LeadConvertResult {
	lead: Lead;
	account_id: number;
	contact_id: number | null;
	opportunity_id: number | null;
}

// ---------- Embudo / Oportunidades ----------
export interface Pipeline {
	id: number;
	name: string;
	is_default: boolean;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export interface Stage {
	id: number;
	pipeline_id: number;
	name: string;
	position: number;
	probability: number;
	is_won: boolean;
	is_lost: boolean;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export interface Opportunity {
	id: number;
	name: string;
	account_id: number | null;
	contact_id: number | null;
	pipeline_id: number | null;
	stage_id: number | null;
	amount: number | null;
	currency: string;
	probability: number | null;
	status: OpportunityStatus;
	expected_close_date: string | null;
	closed_at: string | null;
	won_date: string | null;
	lost_date: string | null;
	lost_reason: string | null;
	source: string | null;
	owner_user_id: string | null;
	notes: string | null;
	operation_type: string | null;
	reference: string | null;
	converted_service_request_id: number | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type OpportunityInput = Partial<Omit<Opportunity, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at' | 'closed_at'>> & {
	name: string;
};

export interface Activity {
	id: number;
	activity_type: ActivityType;
	subject: string;
	description: string | null;
	status: ActivityStatus;
	due_date: string | null;
	completed_at: string | null;
	account_id: number | null;
	contact_id: number | null;
	lead_id: number | null;
	opportunity_id: number | null;
	owner_user_id: string | null;
	tenant_id: number;
	company_id: number;
	created_at: string;
	updated_at: string;
}

export type ActivityInput = Partial<Omit<Activity, 'id' | 'tenant_id' | 'company_id' | 'created_at' | 'updated_at' | 'completed_at'>> & {
	activity_type: ActivityType;
	subject: string;
};

export interface StageMetric {
	stage_id: number;
	stage_name: string;
	position: number;
	count: number;
	value: number;
}

export interface CrmMetrics {
	total_accounts: number;
	total_contacts: number;
	total_leads: number;
	open_leads: number;
	open_opportunities: number;
	open_pipeline_value: number;
	won_opportunities: number;
	won_value: number;
	pending_activities: number;
	by_stage: StageMetric[];
}
