<?php

require_once __DIR__ . '/../config/database.php';

/**
 * Mock Database Class
 * Works without a real database - returns mock data for demo purposes
 */
class Database {
    private static $instance = null;
    private $mockData = [];

    private function __construct() {
        $this->initMockData();
    }

    private function initMockData() {
        // Demo data
        $this->mockData = [
            'users' => [
                ['id' => 1, 'agency_id' => 1, 'name' => 'John Doe', 'email' => 'admin@webbooks.com', 'password' => password_hash('password123', PASSWORD_DEFAULT), 'role' => 'admin']
            ],
            'agencies' => [
                ['id' => 1, 'name' => 'WebBooks Travel Solutions', 'address' => '123 Business Street, Johannesburg', 'phone' => '+27 11 123 4567']
            ],
            'clients' => [
                ['id' => 1, 'agency_id' => 1, 'name' => 'Travel Express Ltd', 'company' => 'Travel Express Ltd', 'email' => 'accounts@travelexpress.com', 'phone' => '+1 555-0123', 'type' => 'corporate', 'status' => 'active', 'total_invoiced' => 125000],
                ['id' => 2, 'agency_id' => 1, 'name' => 'Global Tours Inc', 'company' => 'Global Tours Inc', 'email' => 'billing@globaltours.com', 'phone' => '+1 555-0456', 'type' => 'corporate', 'status' => 'active', 'total_invoiced' => 82000],
                ['id' => 3, 'agency_id' => 1, 'name' => 'Adventure Seekers', 'company' => 'Adventure Seekers', 'email' => 'finance@adventureseekers.com', 'phone' => '+1 555-0789', 'type' => 'corporate', 'status' => 'active', 'total_invoiced' => 158000],
                ['id' => 4, 'agency_id' => 1, 'name' => 'Ocean View Travel', 'company' => 'Ocean View Travel', 'email' => 'accounts@oceanview.com', 'phone' => '+1 555-0321', 'type' => 'sme', 'status' => 'active', 'total_invoiced' => 63000],
                ['id' => 5, 'agency_id' => 1, 'name' => 'Mountain Explorers', 'company' => 'Mountain Explorers', 'email' => 'billing@mountainexplorers.com', 'phone' => '+1 555-0654', 'type' => 'sme', 'status' => 'active', 'total_invoiced' => 91000],
            ],
            'invoices' => [
                ['id' => 1, 'agency_id' => 1, 'client_id' => 1, 'invoice_number' => 'INV-0001', 'amount' => 12500, 'currency' => 'USD', 'issue_date' => '2026-03-01', 'due_date' => '2026-04-01', 'status' => 'paid', 'created_at' => '2026-03-01 10:00:00'],
                ['id' => 2, 'agency_id' => 1, 'client_id' => 2, 'invoice_number' => 'INV-0002', 'amount' => 8200, 'currency' => 'USD', 'issue_date' => '2026-03-04', 'due_date' => '2026-04-04', 'status' => 'pending', 'created_at' => '2026-03-04 11:00:00'],
                ['id' => 3, 'agency_id' => 1, 'client_id' => 3, 'invoice_number' => 'INV-0003', 'amount' => 15800, 'currency' => 'USD', 'issue_date' => '2026-03-04', 'due_date' => '2026-04-04', 'status' => 'paid', 'created_at' => '2026-03-04 12:00:00'],
                ['id' => 4, 'agency_id' => 1, 'client_id' => 4, 'invoice_number' => 'INV-0004', 'amount' => 6300, 'currency' => 'USD', 'issue_date' => '2026-03-03', 'due_date' => '2026-04-03', 'status' => 'overdue', 'created_at' => '2026-03-03 09:00:00'],
            ],
            'expenses' => [
                ['id' => 1, 'agency_id' => 1, 'expense_number' => 'EXP-0001', 'category' => 'Flights', 'description' => 'Airline tickets for Cairo trip', 'vendor' => 'EgyptAir', 'amount' => 4500, 'expense_date' => '2026-03-05', 'status' => 'approved'],
                ['id' => 2, 'agency_id' => 1, 'expense_number' => 'EXP-0002', 'category' => 'Accommodation', 'description' => 'Hotel booking - Marriott', 'vendor' => 'Marriott Hotel', 'amount' => 2200, 'expense_date' => '2026-03-04', 'status' => 'approved'],
                ['id' => 3, 'agency_id' => 1, 'expense_number' => 'EXP-0003', 'category' => 'Transportation', 'description' => 'Airport transfer', 'vendor' => 'Local Taxi', 'amount' => 150, 'expense_date' => '2026-03-04', 'status' => 'pending'],
            ],
            'tickets' => [
                ['id' => 1, 'agency_id' => 1, 'client_id' => 1, 'ticket_number' => '098-1234567', 'pnr' => 'ABC123', 'route' => 'JNB-LHR-JNB', 'airline' => 'British Airways', 'original_amount' => 2500, 'current_value' => 2250, 'status' => 'refundable', 'issue_date' => '2026-02-15', 'expiry_date' => '2026-03-15'],
                ['id' => 2, 'agency_id' => 1, 'client_id' => 2, 'ticket_number' => '098-1234568', 'pnr' => 'DEF456', 'route' => 'JNB-CDX-JNB', 'airline' => 'Air France', 'original_amount' => 3200, 'current_value' => 2880, 'status' => 'unused', 'issue_date' => '2026-02-20', 'expiry_date' => '2026-03-20'],
                ['id' => 3, 'agency_id' => 1, 'client_id' => 3, 'ticket_number' => '098-1234569', 'pnr' => 'GHI789', 'route' => 'JNB-JFK-JNB', 'airline' => 'United Airlines', 'original_amount' => 2800, 'current_value' => 2520, 'status' => 'reissued', 'issue_date' => '2026-02-10', 'expiry_date' => '2026-03-10'],
                ['id' => 4, 'agency_id' => 1, 'client_id' => 4, 'ticket_number' => '098-1234570', 'pnr' => 'JKL012', 'route' => 'CPT-LHR-CPT', 'airline' => 'Virgin Atlantic', 'original_amount' => 1800, 'current_value' => 1620, 'status' => 'unused', 'issue_date' => '2026-02-25', 'expiry_date' => '2026-03-25'],
                ['id' => 5, 'agency_id' => 1, 'client_id' => 5, 'ticket_number' => '098-1234571', 'pnr' => 'MNO345', 'route' => 'JNB-DUB-JNB', 'airline' => 'Emirates', 'original_amount' => 3500, 'current_value' => 3150, 'status' => 'expired', 'issue_date' => '2026-01-05', 'expiry_date' => '2026-02-05'],
            ],
            'bank_accounts' => [
                ['id' => 1, 'agency_id' => 1, 'name' => 'Business Operating Account', 'bank_name' => 'First National Bank', 'account_number' => '1234567890', 'type' => 'checking', 'balance' => 245000, 'currency' => 'USD'],
                ['id' => 2, 'agency_id' => 1, 'name' => 'Travel Agency Reserve', 'bank_name' => 'Standard Chartered', 'account_number' => '0987654321', 'type' => 'savings', 'balance' => 125000, 'currency' => 'USD'],
                ['id' => 3, 'agency_id' => 1, 'name' => 'Client Trust Account', 'bank_name' => 'First National Bank', 'account_number' => '5678901234', 'type' => 'trust', 'balance' => 85000, 'currency' => 'USD'],
            ],
            'bills' => [
                ['id' => 1, 'agency_id' => 1, 'vendor' => 'EgyptAir', 'amount' => 45000, 'due_date' => '2026-03-10', 'priority' => 'high', 'status' => 'pending'],
                ['id' => 2, 'agency_id' => 1, 'vendor' => 'Marriott Hotels', 'amount' => 22500, 'due_date' => '2026-03-15', 'priority' => 'high', 'status' => 'pending'],
                ['id' => 3, 'agency_id' => 1, 'vendor' => 'Hertz Rentals', 'amount' => 8200, 'due_date' => '2026-03-18', 'priority' => 'medium', 'status' => 'pending'],
            ],
            'crm_contacts' => [
                ['id' => 1, 'agency_id' => 1, 'name' => 'John Smith', 'company' => 'Tech Corp Ltd', 'email' => 'john@techcorp.com', 'phone' => '+1 555-0101', 'type' => 'corporate', 'status' => 'won', 'assigned_to' => 1],
                ['id' => 2, 'agency_id' => 1, 'name' => 'Sarah Johnson', 'company' => 'Global Finance', 'email' => 'sarah@globalfin.com', 'phone' => '+1 555-0102', 'type' => 'corporate', 'status' => 'negotiation', 'assigned_to' => 1],
                ['id' => 3, 'agency_id' => 1, 'name' => 'Mike Chen', 'company' => 'Startup Inc', 'email' => 'mike@startup.io', 'phone' => '+1 555-0103', 'type' => 'sme', 'status' => 'qualified', 'assigned_to' => 1],
            ],
            'gds_files' => [
                ['id' => 1, 'agency_id' => 1, 'provider' => 'Amadeus', 'file_name' => 'amadeus_orders_20260305.xml', 'records_count' => 450, 'status' => 'matched', 'uploaded_by' => 1, 'created_at' => '2026-03-05 06:00:00'],
                ['id' => 2, 'agency_id' => 1, 'provider' => 'Sabre', 'file_name' => 'sabre_pnr_export_20260305.csv', 'records_count' => 320, 'status' => 'parsed', 'uploaded_by' => 1, 'created_at' => '2026-03-05 05:30:00'],
            ],
            'bsp_imports' => [
                ['id' => 1, 'agency_id' => 1, 'source' => 'IATA BSP Africa', 'file_name' => 'bsp_africa_20260305.csv', 'records_count' => 1250, 'total_amount' => 285000, 'status' => 'matched', 'imported_at' => '2026-03-05 08:30:00'],
            ]
        ];
    }

    public static function getInstance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function select($sql, $params = []) {
        // Simple mock - parse SQL to determine what to return
        $sqlLower = strtolower($sql);
        
        if (strpos($sqlLower, 'from invoices') !== false) {
            return $this->mockData['invoices'];
        }
        if (strpos($sqlLower, 'from expenses') !== false) {
            return $this->mockData['expenses'];
        }
        if (strpos($sqlLower, 'from clients') !== false) {
            return $this->mockData['clients'];
        }
        if (strpos($sqlLower, 'from tickets') !== false) {
            return $this->mockData['tickets'];
        }
        if (strpos($sqlLower, 'from bank_accounts') !== false) {
            return $this->mockData['bank_accounts'];
        }
        if (strpos($sqlLower, 'from bills') !== false) {
            return $this->mockData['bills'];
        }
        if (strpos($sqlLower, 'from crm_contacts') !== false) {
            return $this->mockData['crm_contacts'];
        }
        if (strpos($sqlLower, 'from gds_files') !== false) {
            return $this->mockData['gds_files'];
        }
        if (strpos($sqlLower, 'from bsp_imports') !== false) {
            return $this->mockData['bsp_imports'];
        }
        if (strpos($sqlLower, 'from users') !== false) {
            return $this->mockData['users'];
        }
        
        return [];
    }

    public function selectOne($sql, $params = []) {
        $results = $this->select($sql, $params);
        return $results[0] ?? null;
    }

    public function insert($table, $data) {
        $id = count($this->mockData[$table] ?? []) + 1;
        $data['id'] = $id;
        $data['created_at'] = date('Y-m-d H:i:s');
        $this->mockData[$table][] = $data;
        return $id;
    }

    public function update($table, $data, $where, $whereParams = []) {
        return true;
    }

    public function delete($table, $where, $params = []) {
        return true;
    }

    public function getConnection() {
        return null;
    }
}

function initDatabase() {
    // No-op for mock database
}
