<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // 1. Seed Company Tenants
        $companies = [
            [
                'id' => 'COMP-001',
                'name' => 'Teletalk Corp Ltd',
                'logo' => '⚡',
                'subscription' => 'Enterprise',
                'status' => 'Active',
                'phone' => '+88029555111',
                'email' => 'info@teletalkcorp.com',
                'address' => 'Gulshan-2, Dhaka 1212',
                'currency' => '৳',
                'tax_number' => 'BIN-19284756302',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'COMP-002',
                'name' => 'Globe Freight Systems',
                'logo' => '🌐',
                'subscription' => 'Premium',
                'status' => 'Active',
                'phone' => '+14155552671',
                'email' => 'finance@globefreight.com',
                'address' => 'Sansom St, San Francisco, CA',
                'currency' => '$',
                'tax_number' => 'EIN-99238812',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        ];

        foreach ($companies as $company) {
            DB::table('companies')->updateOrInsert(['id' => $company['id']], $company);
        }

        // 2. Seed Operating cash point Branches
        $branches = [
            [
                'id' => 'BR-101',
                'company_id' => 'COMP-001',
                'name' => 'Head Office (Dhaka)',
                'code' => 'HQ-DKA',
                'balance' => 1258750.00,
                'opening_balance' => 1000000.00,
                'manager_name' => 'Ariful Islam',
                'status' => 'Active',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'BR-102',
                'company_id' => 'COMP-001',
                'name' => 'Uttara Branch',
                'code' => 'BR-UTR',
                'balance' => 458750.00,
                'opening_balance' => 400000.00,
                'manager_name' => 'Tasnim Jahan',
                'status' => 'Active',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'BR-103',
                'company_id' => 'COMP-001',
                'name' => 'Dhanmondi Branch',
                'code' => 'BR-DMD',
                'balance' => 325500.00,
                'opening_balance' => 300000.00,
                'manager_name' => 'Imran Chowdhury',
                'status' => 'Active',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        ];

        foreach ($branches as $branch) {
            DB::table('branches')->updateOrInsert(['id' => $branch['id']], $branch);
        }

        // 3. Seed Users with multi-company, Multi-branch bindings
        $users = [
            [
                'id' => 'USR-001',
                'company_id' => 'COMP-001',
                'branch_id' => 'BR-101',
                'name' => 'M. Ashikur Rahman',
                'email' => 'mdashikurcse@gmail.com',
                'password' => Hash::make('password'),
                'role' => 'Super Admin',
                'is_active' => true,
                'photo_url' => 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?auto=format&fit=crop&w=150&q=80',
                'phone' => '+8801700000001',
                'designation' => 'SaaS platform Architect',
                'bio' => 'Head of global cash flow, cloud deployment & multi-tenant security structures.',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'USR-002',
                'company_id' => 'COMP-001',
                'branch_id' => 'BR-101',
                'name' => 'Rashedul Hasan',
                'email' => 'rashed@teletalkcorp.com',
                'password' => Hash::make('password'),
                'role' => 'Company Admin',
                'is_active' => true,
                'photo_url' => 'https://images.unsplash.com/photo-1570295999919-56ceb5ecca61?auto=format&fit=crop&w=150&q=80',
                'phone' => '+8801811112222',
                'designation' => 'Finance Director',
                'bio' => 'Managing corporate audits, legal tax clearances, and organizational branch liquidity controllers.',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'USR-003',
                'company_id' => 'COMP-001',
                'branch_id' => 'BR-102',
                'name' => 'Anika Tabassum',
                'email' => 'cashier@teletalkcorp.com',
                'password' => Hash::make('password'),
                'role' => 'Cashier',
                'is_active' => true,
                'photo_url' => 'https://images.unsplash.com/photo-1494790108377-be9c29b29330?auto=format&fit=crop&w=150&q=80',
                'phone' => '+8801999888777',
                'designation' => 'Senior Petty Cash Cashier',
                'bio' => 'Registrar of cash payments & operational vouchers.',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'USR-004',
                'company_id' => 'COMP-001',
                'branch_id' => 'BR-101',
                'name' => 'Kazi Mahbub',
                'email' => 'auditor@teletalkcorp.com',
                'password' => Hash::make('password'),
                'role' => 'Auditor',
                'is_active' => true,
                'photo_url' => 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?auto=format&fit=crop&w=150&q=80',
                'phone' => '+8801555000999',
                'designation' => 'Chief General Ledger Accountant',
                'bio' => 'Supervisor of monthly ledger reconciliations and financial bank adjustment worksheets.',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        ];

        foreach ($users as $user) {
            DB::table('users')->updateOrInsert(['id' => $user['id']], $user);
        }

        // 4. Seed Corporate Bank Account Registers
        $bankAccounts = [
            [
                'id' => 'BNK-001',
                'company_id' => 'COMP-001',
                'bank_name' => 'Sonali Bank PLC',
                'account_name' => 'Teletalk Principal Operating',
                'account_number' => '0021-992384-012',
                'branch' => 'Motijheel Corporate Branch',
                'balance' => 4500000.00,
                'status' => 'Active',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'BNK-002',
                'company_id' => 'COMP-001',
                'bank_name' => 'Dutch-Bangla Bank',
                'account_name' => 'Teletalk Petty Cash Pool',
                'account_number' => '122.105.992834',
                'branch' => 'Gulshan Branch',
                'balance' => 1250000.00,
                'status' => 'Active',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        ];

        foreach ($bankAccounts as $bank) {
            DB::table('bank_accounts')->updateOrInsert(['id' => $bank['id']], $bank);
        }

        // 5. Seed Core Cash Transactions
        $transactions = [
            [
                'id' => 'TX-201',
                'company_id' => 'COMP-001',
                'branch_id' => 'BR-101',
                'voucher_no' => 'VCH-2026-601',
                'type' => 'Cash In',
                'amount' => 12500.00,
                'particulars' => 'Utility reimbursement refund received from tenant floor',
                'category' => 'Miscellaneous Revenue',
                'payment_method' => 'Cash',
                'bank_account_id' => null,
                'reference_no' => 'REF-9923',
                'status' => 'Approved',
                'created_by_name' => 'Anika Tabassum',
                'created_by_id' => 'USR-003',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id' => 'TX-202',
                'company_id' => 'COMP-001',
                'branch_id' => 'BR-101',
                'voucher_no' => 'VCH-2026-602',
                'type' => 'Cash Out',
                'amount' => 45000.00,
                'particulars' => 'Purchase of server UPS backup for HQ rack server',
                'category' => 'Office Equipment',
                'payment_method' => 'Cash',
                'bank_account_id' => null,
                'reference_no' => 'REF-1122',
                'status' => 'Approved',
                'created_by_name' => 'Anika Tabassum',
                'created_by_id' => 'USR-003',
                'created_at' => now(),
                'updated_at' => now(),
            ]
        ];

        foreach ($transactions as $item) {
            DB::table('cash_transactions')->updateOrInsert(['id' => $item['id']], $item);
        }

        // 6. Seed Categories
        $categories = [
            // Expense Categories
            ['id' => 'CAT-001', 'company_id' => 'COMP-001', 'name' => 'Premises Rent', 'type' => 'Expense', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-002', 'company_id' => 'COMP-001', 'name' => 'Electricity Bill', 'type' => 'Expense', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-003', 'company_id' => 'COMP-001', 'name' => 'Broadband & Telecom', 'type' => 'Expense', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-004', 'company_id' => 'COMP-001', 'name' => 'Stationery Inventory', 'type' => 'Expense', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-005', 'company_id' => 'COMP-001', 'name' => 'Hardware Maintenance', 'type' => 'Expense', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-006', 'company_id' => 'COMP-001', 'name' => 'Staff Refreshment', 'type' => 'Expense', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            // Income Categories
            ['id' => 'CAT-007', 'company_id' => 'COMP-001', 'name' => 'Sales Income', 'type' => 'Income', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-008', 'company_id' => 'COMP-001', 'name' => 'Consultancy Service Charges', 'type' => 'Income', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-009', 'company_id' => 'COMP-001', 'name' => 'Lease Revenue', 'type' => 'Income', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-010', 'company_id' => 'COMP-001', 'name' => 'Asset Disposal', 'type' => 'Income', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 'CAT-011', 'company_id' => 'COMP-001', 'name' => 'Owner Investments', 'type' => 'Income', 'status' => 'Active', 'created_at' => now(), 'updated_at' => now()]
        ];

        foreach ($categories as $cat) {
            DB::table('categories')->updateOrInsert(['id' => $cat['id']], $cat);
        }
    }
}
