<?php

namespace Database\Seeders\Dummy;

use App\Models\Orders\Client;
use App\Models\Tenants\BankAccount;
use App\Models\Tenants\Bill;
use App\Models\Tenants\BillInstallment;
use Database\Seeders\DummyDataSeeder;
use Illuminate\Database\Seeder;

class DummyBillSeeder extends Seeder
{
    public function run(): void
    {
        $tenantId = DummyDataSeeder::TENANT_ID;
        $userId = DummyDataSeeder::USER_ID;

        // 기존 데이터 있으면 스킵
        $existing = Bill::where('tenant_id', $tenantId)->count();
        if ($existing > 0) {
            $this->command->info('   ⚠ bills: 이미 '.$existing.'건 존재 (스킵)');

            return;
        }

        // 거래처 매핑
        $clients = Client::where('tenant_id', $tenantId)->get()->keyBy('name');

        // 은행계좌 (대표계좌)
        $primaryBankId = BankAccount::where('tenant_id', $tenantId)
            ->where('is_primary', true)
            ->value('id');

        // 수취 어음 데이터 (received) - 15건
        $receivedBills = [
            ['bill_number' => '202501000001', 'client' => '삼성전자',      'amount' => 50000000, 'issue_date' => '2025-01-15', 'maturity_date' => '2025-04-15', 'status' => 'paymentComplete'],
            ['bill_number' => '202501000002', 'client' => 'LG전자',        'amount' => 35000000, 'issue_date' => '2025-02-10', 'maturity_date' => '2025-05-10', 'status' => 'paymentComplete'],
            ['bill_number' => '202502000001', 'client' => 'SK하이닉스',    'amount' => 80000000, 'issue_date' => '2025-02-20', 'maturity_date' => '2025-05-20', 'status' => 'paymentComplete'],
            ['bill_number' => '202503000001', 'client' => '현대자동차',    'amount' => 45000000, 'issue_date' => '2025-03-05', 'maturity_date' => '2025-06-05', 'status' => 'maturityResult'],
            ['bill_number' => '202504000001', 'client' => '네이버',        'amount' => 25000000, 'issue_date' => '2025-04-12', 'maturity_date' => '2025-07-12', 'status' => 'maturityResult'],
            ['bill_number' => '202505000001', 'client' => '카카오',        'amount' => 18000000, 'issue_date' => '2025-05-08', 'maturity_date' => '2025-08-08', 'status' => 'stored'],
            ['bill_number' => '202506000001', 'client' => '쿠팡',          'amount' => 32000000, 'issue_date' => '2025-06-15', 'maturity_date' => '2025-09-15', 'status' => 'stored'],
            ['bill_number' => '202507000001', 'client' => '삼성SDS',       'amount' => 65000000, 'issue_date' => '2025-07-20', 'maturity_date' => '2025-10-20', 'status' => 'stored'],
            ['bill_number' => '202508000001', 'client' => '토스',          'amount' => 15000000, 'issue_date' => '2025-08-10', 'maturity_date' => '2025-11-10', 'status' => 'stored'],
            ['bill_number' => '202509000001', 'client' => '두산에너빌리티', 'amount' => 55000000, 'issue_date' => '2025-09-05', 'maturity_date' => '2025-12-05', 'status' => 'maturityAlert'],
            ['bill_number' => '202510000001', 'client' => '삼성전자',      'amount' => 42000000, 'issue_date' => '2025-10-15', 'maturity_date' => '2026-01-15', 'status' => 'stored'],
            ['bill_number' => '202511000001', 'client' => 'LG전자',        'amount' => 28000000, 'issue_date' => '2025-11-08', 'maturity_date' => '2026-02-08', 'status' => 'stored'],
            ['bill_number' => '202511000002', 'client' => '네이버',        'amount' => 38000000, 'issue_date' => '2025-11-20', 'maturity_date' => '2026-02-20', 'status' => 'stored'],
            ['bill_number' => '202512000001', 'client' => '현대자동차',    'amount' => 52000000, 'issue_date' => '2025-12-10', 'maturity_date' => '2026-03-10', 'status' => 'stored'],
            ['bill_number' => '202512000002', 'client' => 'SK하이닉스',    'amount' => 70000000, 'issue_date' => '2025-12-18', 'maturity_date' => '2026-03-18', 'status' => 'stored'],
        ];

        // 발행 어음 데이터 (issued) - 15건
        $issuedBills = [
            ['bill_number' => '202501100001', 'client' => '한화솔루션',    'amount' => 40000000, 'issue_date' => '2025-01-20', 'maturity_date' => '2025-04-20', 'status' => 'collectionComplete'],
            ['bill_number' => '202502100001', 'client' => '포스코',        'amount' => 55000000, 'issue_date' => '2025-02-15', 'maturity_date' => '2025-05-15', 'status' => 'collectionComplete'],
            ['bill_number' => '202503100001', 'client' => '롯데케미칼',    'amount' => 30000000, 'issue_date' => '2025-03-10', 'maturity_date' => '2025-06-10', 'status' => 'collectionComplete'],
            ['bill_number' => '202504100001', 'client' => 'GS칼텍스',      'amount' => 22000000, 'issue_date' => '2025-04-18', 'maturity_date' => '2025-07-18', 'status' => 'collectionComplete'],
            ['bill_number' => '202505100001', 'client' => '대한항공',      'amount' => 18000000, 'issue_date' => '2025-05-12', 'maturity_date' => '2025-08-12', 'status' => 'collectionRequest'],
            ['bill_number' => '202506100001', 'client' => '현대제철',      'amount' => 48000000, 'issue_date' => '2025-06-20', 'maturity_date' => '2025-09-20', 'status' => 'collectionRequest'],
            ['bill_number' => '202507100001', 'client' => 'SK이노베이션',  'amount' => 35000000, 'issue_date' => '2025-07-15', 'maturity_date' => '2025-10-15', 'status' => 'stored'],
            ['bill_number' => '202508100001', 'client' => 'CJ대한통운',    'amount' => 25000000, 'issue_date' => '2025-08-22', 'maturity_date' => '2025-11-22', 'status' => 'stored'],
            ['bill_number' => '202509100001', 'client' => '두산에너빌리티', 'amount' => 60000000, 'issue_date' => '2025-09-10', 'maturity_date' => '2025-12-10', 'status' => 'maturityAlert'],
            ['bill_number' => '202510100001', 'client' => '한화솔루션',    'amount' => 45000000, 'issue_date' => '2025-10-08', 'maturity_date' => '2026-01-08', 'status' => 'stored'],
            ['bill_number' => '202511100001', 'client' => '포스코',        'amount' => 58000000, 'issue_date' => '2025-11-05', 'maturity_date' => '2026-02-05', 'status' => 'stored'],
            ['bill_number' => '202511100002', 'client' => '롯데케미칼',    'amount' => 32000000, 'issue_date' => '2025-11-18', 'maturity_date' => '2026-02-18', 'status' => 'stored'],
            ['bill_number' => '202512100001', 'client' => 'GS칼텍스',      'amount' => 28000000, 'issue_date' => '2025-12-05', 'maturity_date' => '2026-03-05', 'status' => 'stored'],
            ['bill_number' => '202512100002', 'client' => '현대제철',      'amount' => 42000000, 'issue_date' => '2025-12-15', 'maturity_date' => '2026-03-15', 'status' => 'stored'],
            ['bill_number' => '202512100003', 'client' => 'SK이노베이션',  'amount' => 38000000, 'issue_date' => '2025-12-22', 'maturity_date' => '2026-03-22', 'status' => 'stored'],
        ];

        // 차수 관리 데이터
        $installmentsData = [
            '202501000001' => [
                ['date' => '2025-02-15', 'amount' => 25000000, 'note' => '1차 분할 입금'],
                ['date' => '2025-03-15', 'amount' => 25000000, 'note' => '2차 분할 입금'],
            ],
            '202502000001' => [
                ['date' => '2025-03-20', 'amount' => 40000000, 'note' => '1차 분할 입금'],
                ['date' => '2025-04-20', 'amount' => 40000000, 'note' => '2차 분할 입금'],
            ],
            '202507000001' => [
                ['date' => '2025-08-20', 'amount' => 30000000, 'note' => '1차 분할 입금'],
                ['date' => '2025-09-20', 'amount' => 35000000, 'note' => '2차 분할 입금'],
            ],
            '202501100001' => [
                ['date' => '2025-02-20', 'amount' => 20000000, 'note' => '1차 분할 지급'],
                ['date' => '2025-03-20', 'amount' => 20000000, 'note' => '2차 분할 지급'],
            ],
            '202502100001' => [
                ['date' => '2025-03-15', 'amount' => 27500000, 'note' => '1차 분할 지급'],
                ['date' => '2025-04-15', 'amount' => 27500000, 'note' => '2차 분할 지급'],
            ],
            '202506100001' => [
                ['date' => '2025-07-20', 'amount' => 24000000, 'note' => '1차 분할 지급'],
                ['date' => '2025-08-20', 'amount' => 24000000, 'note' => '2차 분할 지급'],
            ],
        ];

        $billCount = 0;
        $installmentCount = 0;

        // 수취 어음 생성
        foreach ($receivedBills as $data) {
            $client = $clients->get($data['client']);
            $bill = Bill::create([
                'tenant_id' => $tenantId,
                'bill_number' => $data['bill_number'],
                'bill_type' => 'received',
                'client_id' => $client?->id,
                'client_name' => $client ? null : $data['client'],
                'amount' => $data['amount'],
                'issue_date' => $data['issue_date'],
                'maturity_date' => $data['maturity_date'],
                'status' => $data['status'],
                'is_electronic' => rand(0, 1) === 1,
                'bank_account_id' => $primaryBankId,
                'installment_count' => isset($installmentsData[$data['bill_number']]) ? count($installmentsData[$data['bill_number']]) : 0,
                'created_by' => $userId,
            ]);
            $billCount++;

            // 차수 관리 데이터 추가
            if (isset($installmentsData[$data['bill_number']])) {
                foreach ($installmentsData[$data['bill_number']] as $instData) {
                    BillInstallment::create([
                        'bill_id' => $bill->id,
                        'installment_date' => $instData['date'],
                        'amount' => $instData['amount'],
                        'note' => $instData['note'],
                    ]);
                    $installmentCount++;
                }
            }
        }

        // 발행 어음 생성
        foreach ($issuedBills as $data) {
            $client = $clients->get($data['client']);
            $bill = Bill::create([
                'tenant_id' => $tenantId,
                'bill_number' => $data['bill_number'],
                'bill_type' => 'issued',
                'client_id' => $client?->id,
                'client_name' => $client ? null : $data['client'],
                'amount' => $data['amount'],
                'issue_date' => $data['issue_date'],
                'maturity_date' => $data['maturity_date'],
                'status' => $data['status'],
                'is_electronic' => rand(0, 1) === 1,
                'bank_account_id' => $primaryBankId,
                'installment_count' => isset($installmentsData[$data['bill_number']]) ? count($installmentsData[$data['bill_number']]) : 0,
                'created_by' => $userId,
            ]);
            $billCount++;

            // 차수 관리 데이터 추가
            if (isset($installmentsData[$data['bill_number']])) {
                foreach ($installmentsData[$data['bill_number']] as $instData) {
                    BillInstallment::create([
                        'bill_id' => $bill->id,
                        'installment_date' => $instData['date'],
                        'amount' => $instData['amount'],
                        'note' => $instData['note'],
                    ]);
                    $installmentCount++;
                }
            }
        }

        $this->command->info('   ✓ bills: '.$billCount.'건 생성');
        $this->command->info('   ✓ bill_installments: '.$installmentCount.'건 생성');
    }
}
