import apiClient from '../../../shared/services/apiClient';

// Chart of Accounts
export interface Account {
  id: number;
  account_number: string;
  account_name: string;
  account_type: string;
  account_category: string;
  normal_balance: string;
  description?: string;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface CreateAccountRequest {
  account_number: string;
  account_name: string;
  account_type: string;
  account_category: string;
  normal_balance: string;
  description?: string;
  is_active?: boolean;
}

// Fiscal Periods
export interface FiscalPeriod {
  id: number;
  name: string;
  start_date: string;
  end_date: string;
  status: 'Open' | 'Closed' | 'Locked';
  is_current: boolean;
  created_at: string;
  updated_at: string;
}

export interface CreateFiscalPeriodRequest {
  name: string;
  start_date: string;
  end_date: string;
  set_as_current?: boolean;
}

// Journal Entries
export interface JournalLine {
  account_id: number;
  debit: number;
  credit: number;
  description?: string;
}

export interface JournalEntry {
  id: number;
  entry_date: string;
  description: string;
  entry_type: string;
  reference_id?: string;
  fiscal_period_id: number;
  created_at: string;
  updated_at: string;
  journal_lines: Array<JournalLine & { id: number; account: Account }>;
}

export interface CreateJournalEntryRequest {
  entry_date: string;
  description: string;
  entry_type: string;
  fiscal_period_id: number;
  lines: JournalLine[];
  reference_id?: string;
  requires_approval?: boolean;
  request_reason?: string;
}

// Trial Balance
export interface TrialBalance {
  accounts: Array<{
    account_number: string;
    account_name: string;
    account_type: string;
    normal_balance: string;
    debit: number;
    credit: number;
  }>;
  total_debits: number;
  total_credits: number;
  is_balanced: boolean;
}

class GLService {
  // Chart of Accounts
  async getAccounts(): Promise<{ status: string; data: { accounts: Account[] } }> {
    const response = await apiClient.get('/financial/gl/accounts');
    return response.data;
  }

  async createAccount(data: CreateAccountRequest): Promise<{ status: string; data: Account }> {
    const response = await apiClient.post('/financial/gl/accounts', data);
    return response.data;
  }

  // Fiscal Periods
  async getFiscalPeriods(): Promise<{ status: string; data: { fiscal_periods: FiscalPeriod[] } }> {
    const response = await apiClient.get('/financial/gl/fiscal-periods');
    return response.data;
  }

  async createFiscalPeriod(data: CreateFiscalPeriodRequest): Promise<{ status: string; data: FiscalPeriod }> {
    const response = await apiClient.post('/financial/gl/fiscal-periods', data);
    return response.data;
  }

  async closeFiscalPeriod(periodId: number, lockPeriod: boolean = false, reason?: string): Promise<{ status: string; data: FiscalPeriod }> {
    const response = await apiClient.put(`/financial/gl/fiscal-periods/${periodId}/close`, {
      lock_period: lockPeriod,
      request_reason: reason
    });
    return response.data;
  }

  // Journal Entries
  async getJournalEntries(params?: {
    fiscal_period_id?: number;
    start_date?: string;
    end_date?: string;
    entry_type?: string;
    account_id?: number;
    page?: number;
    limit?: number;
  }): Promise<{ status: string; data: { journal_entries: JournalEntry[] } }> {
    const response = await apiClient.get('/financial/gl/journal-entries', { params });
    return response.data;
  }

  async createJournalEntry(data: CreateJournalEntryRequest): Promise<{ status: string; data: JournalEntry }> {
    const response = await apiClient.post('/financial/gl/journal-entries', data);
    return response.data;
  }

  // Trial Balance
  async getTrialBalance(fiscalPeriodId?: number, asOfDate?: string): Promise<{ status: string; data: TrialBalance }> {
    const params: Record<string, unknown> = {};
    if (fiscalPeriodId) params.fiscal_period_id = fiscalPeriodId;
    if (asOfDate) params.as_of_date = asOfDate;
    
    const response = await apiClient.get('/financial/gl/trial-balance', { params });
    return response.data;
  }
}

const glService = new GLService();
export default glService;

