<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class DemoSeeder extends Seeder
{
    public function run()
    {
        // Insert default company (S.A.T.T)
        $companyData = [
            'id' => 1,
            'name' => 'S.A.T.T',
            'full_name' => 'Société d'Assistance Transit et Transport',
            'address' => '24 Rue Daghbagi, 3ème étage, B N°5',
            'city' => 'Tunis',
            'country' => 'Tunisie',
            'phone' => '71339107',
            'fax' => '71339119',
            'gsm' => '98268382',
            'email' => 'lassaadaoun.satt@gmail.com',
            'tax_id' => '1005360 CMA 000',
            'currency' => 'TND',
            'default_vat_rate' => 0.19,
            'invoice_prefix' => '080',
            'invoice_start_number' => 1,
            'payment_terms' => 30,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        $this->db->table('companies')->insert($companyData);

        // Insert admin user (password: admin123)
        $userData = [
            'id' => 1,
            'email' => 'admin@satt.tn',
            'password_hash' => password_hash('admin123', PASSWORD_BCRYPT),
            'first_name' => 'Administrateur',
            'last_name' => 'S.A.T.T',
            'phone' => '98268382',
            'role' => 'admin',
            'is_active' => 1,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        $this->db->table('users')->insert($userData);

        // Insert demo client (COMNET)
        $clientData = [
            'id' => 1,
            'company_id' => 1,
            'code' => 'CLI-004',
            'company_name' => 'COMNET',
            'contact_name' => 'Direction COMNET',
            'email' => 'contact@comnet.tn',
            'phone' => '71123456',
            'address' => 'Tunis',
            'city' => 'Tunis',
            'country' => 'Tunisie',
            'payment_terms' => 30,
            'is_active' => 1,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        $this->db->table('clients')->insert($clientData);

        // Insert demo invoice matching the Excel file
        $invoiceData = [
            'id' => 1,
            'company_id' => 1,
            'client_id' => 1,
            'user_id' => 1,
            'invoice_number' => '080/2026',
            'invoice_type' => 'facture',
            'status' => 'payee',
            'date' => '2026-05-07',
            'due_date' => '2026-06-06',
            'reference_client' => '',
            'customs_code' => '',
            'reper_number' => '3401',
            'designation_global' => 'MATERIEL ELECTRIQUE',
            'gross_weight' => 15080.0000,
            'weight_unit' => 'KGS',
            'package_count' => 47,
            'currency_value' => 80373.8400,
            'currency_unit' => 'EUR',
            'shipper' => 'LE GRAND',
            'declaration_number' => '',
            'regime' => '',
            'quittance_number' => '',
            'quittance_amount' => null,
            'subtotal' => 392.0000,
            'discount_amount' => 0.0000,
            'discount_percent' => 0.00,
            'vat_base' => 392.0000,
            'vat_amount' => 74.4800,
            'vat_rate' => 0.1900,
            'stamp_duty' => 1.0000,
            'total_ht' => 392.0000,
            'total_ttc' => 467.4800,
            'advance_amount' => 0.0000,
            'net_payable' => 467.4800,
            'amount_in_words' => 'QUATRE CENT SOIXANTE SEPT DINARS ET QUATRE CENT QUATRE VINGT MILLIMES',
            'notes' => '',
            'terms' => '',
            'paid_at' => '2026-05-07 00:00:00',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        $this->db->table('invoices')->insert($invoiceData);

        // Insert invoice lines matching the Excel
        $lines = [
            ['invoice_id' => 1, 'line_type' => 'frais', 'sort_order' => 1, 'designation' => 'Formalités', 'quantity' => 1, 'unit' => 'U', 'unit_price' => 50.0000, 'amount' => 50.0000, 'is_taxable' => 0],
            ['invoice_id' => 1, 'line_type' => 'frais', 'sort_order' => 2, 'designation' => 'Frais informatique', 'quantity' => 1, 'unit' => 'U', 'unit_price' => 50.0000, 'amount' => 50.0000, 'is_taxable' => 0],
            ['invoice_id' => 1, 'line_type' => 'frais', 'sort_order' => 3, 'designation' => 'Timbre et permis', 'quantity' => 1, 'unit' => 'U', 'unit_price' => 12.0000, 'amount' => 12.0000, 'is_taxable' => 0],
            ['invoice_id' => 1, 'line_type' => 'frais', 'sort_order' => 4, 'designation' => 'Honoraire d'agrée en Douanes', 'quantity' => 1, 'unit' => 'U', 'unit_price' => 200.0000, 'amount' => 200.0000, 'is_taxable' => 0],
            ['invoice_id' => 1, 'line_type' => 'frais', 'sort_order' => 5, 'designation' => 'Visite Douane', 'quantity' => 1, 'unit' => 'U', 'unit_price' => 40.0000, 'amount' => 40.0000, 'is_taxable' => 0],
            ['invoice_id' => 1, 'line_type' => 'frais', 'sort_order' => 6, 'designation' => 'Autres frais (contrôle technique)', 'quantity' => 1, 'unit' => 'U', 'unit_price' => 40.0000, 'amount' => 40.0000, 'is_taxable' => 0],
        ];
        $this->db->table('invoice_lines')->insertBatch($lines);

        // Insert payment
        $paymentData = [
            'id' => 1,
            'invoice_id' => 1,
            'amount' => 467.4800,
            'payment_date' => '2026-05-07',
            'payment_method' => 'virement',
            'reference' => 'Paiement intégral',
            'created_by' => 1,
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $this->db->table('payments')->insert($paymentData);

        echo "Demo data seeded successfully!\n";
        echo "Login: admin@satt.tn / admin123\n";
    }
}
