// Savings-related TypeScript interfaces

export type SavingsStatus = 'active' | 'dormant' | 'closed' | 'frozen';
export type TransactionType =
    | 'deposit'
    | 'withdrawal'
    | 'transfer_in'
    | 'transfer_out'
    | 'correction';

export interface SavingsProduct {
    id: number;
    code: string;
    name: string;
    description: string | null;
    type: 'wadiah';
    minimum_initial_deposit: number;
    minimum_balance: number;
    requires_initial_deposit: boolean;
    administrative_fee: number;
    has_administrative_fee: boolean;
    is_active: boolean;
    created_at: string | null;
    updated_at: string | null;
    deleted_at: string | null;
}

export interface SavingsAccount {
    id: number;
    account_number: string;
    legacy_account_number: string | null;
    member_id: number;
    branch_id: number;
    savings_product_id: number;
    account_name: string;
    balance: number;
    hold_amount: number;
    status: SavingsStatus;
    opened_date: string;
    closed_date: string | null;
    notes: string | null;
    available_balance?: number; // Frontend computed
    transactions_max_transaction_date: string | null; // from withMax()
    created_at: string | null;
    updated_at: string | null;
    deleted_at: string | null;
    created_by: number | null;
    updated_by: number | null;

    // Relationships
    member: {
        id: number;
        id_number: string | null;
        name: string;
        phone: string | null;
    };
    branch: {
        id: number;
        name: string;
    };
    savingsProduct: {
        id: number;
        code: string;
        name: string;
    };
    transactions?: SavingsTransaction[];
}

export interface SavingsTransaction {
    id: number;
    transaction_number: string;
    savings_account_id: number;
    branch_id: number;
    journal_entry_id: number | null;
    type: TransactionType;
    amount: number;
    balance_before: number;
    balance_after: number;
    transaction_date: string;
    description: string | null;
    notes: string | null;
    is_posted: boolean;
    posted_at: string | null;
    created_at: string | null;
    updated_at: string | null;
    deleted_at: string | null;
    created_by: number | null;
    updated_by: number | null;

    // Optional relationships
    account?: {
        id: number;
        account_number: string;
    };
}

export interface SavingsAccountStats {
    total_accounts: number;
    active_accounts: number;
    total_balance: number;
}

export interface SavingsFilters {
    search?: string;
    branch_id?: string;
    product_id?: string;
    status?: string;
}

export interface TransactionFilters {
    type?: TransactionType | 'all';
    date_from?: string;
    date_to?: string;
    is_posted?: string;
}

// Form data types
export interface DepositFormData {
    account_id: number;
    amount: number;
    description?: string;
}

export interface WithdrawalFormData {
    account_id: number;
    amount: number;
    description?: string;
}

export interface OpenAccountFormData {
    member_id: number;
    product_id: number;
    account_name: string;
}

export interface CloseAccountFormData {
    reason: string;
}

export const transactionTypeConfig = {
    deposit: {
        label: 'Setoran',
        color: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
        icon: '↓',
    },
    withdrawal: {
        label: 'Penarikan',
        color: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
        icon: '↑',
    },
    transfer_in: {
        label: 'Transfer Masuk',
        color: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
        icon: '→',
    },
    transfer_out: {
        label: 'Transfer Keluar',
        color: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
        icon: '←',
    },
    correction: {
        label: 'Koreksi',
        color: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
        icon: '✓',
    },
} as const;
