<?php

namespace Database\Seeders;

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

class BankAccountSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 테넌트 ID 1 (코드보잇지엑스) 기준으로 테스트 데이터 생성
        $tenantId = 1;
        $userId = 1; // 관리자

        // 계좌 데이터 (기존 테이블 구조에 맞게 수정)
        $accounts = [
            [
                'tenant_id' => $tenantId,
                'bank_code' => '004',
                'bank_name' => '국민은행',
                'account_number' => '123-456-789012',
                'account_holder' => '주식회사 코드보잇지엑스',
                'account_name' => '주거래 계좌',
                'account_type' => '보통예금',
                'balance' => 450000000, // 4.5억
                'currency' => 'KRW',
                'opened_at' => '2023-01-15',
                'last_transaction_at' => '2026-01-15 14:30:00',
                'branch_name' => '역삼지점',
                'memo' => '주거래 계좌',
                'status' => 'active',
                'is_primary' => true,
                'sort_order' => 1,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_code' => '088',
                'bank_name' => '신한은행',
                'account_number' => '234-567-890123',
                'account_holder' => '주식회사 코드보잇지엑스',
                'account_name' => '법인카드 결제',
                'account_type' => '법인카드 출금',
                'balance' => 90000000, // 9천만원
                'currency' => 'KRW',
                'opened_at' => '2023-01-15',
                'last_transaction_at' => '2026-01-15 11:20:00',
                'branch_name' => '강남지점',
                'memo' => '법인카드 결제 계좌',
                'status' => 'active',
                'is_primary' => false,
                'sort_order' => 2,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_code' => '020',
                'bank_name' => '우리은행',
                'account_number' => '345-678-901234',
                'account_holder' => '주식회사 코드보잇지엑스',
                'account_name' => '정기예금',
                'account_type' => '정기예금',
                'balance' => 200000000, // 2억
                'currency' => 'KRW',
                'opened_at' => '2024-06-01',
                'last_transaction_at' => '2026-01-01 09:00:00',
                'branch_name' => '테헤란로지점',
                'memo' => '정기예금 (만기 2027-06-01)',
                'status' => 'active',
                'is_primary' => false,
                'sort_order' => 3,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_code' => '081',
                'bank_name' => '하나은행',
                'account_number' => '456-789-012345',
                'account_holder' => '주식회사 코드보잇지엑스',
                'account_name' => '급여계좌',
                'account_type' => '보통예금',
                'balance' => 75000000, // 7천5백만원
                'currency' => 'KRW',
                'opened_at' => '2023-03-20',
                'last_transaction_at' => '2026-01-14 16:45:00',
                'branch_name' => '선릉역지점',
                'memo' => '급여 지급 계좌',
                'status' => 'active',
                'is_primary' => false,
                'sort_order' => 4,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_code' => '011',
                'bank_name' => '농협은행',
                'account_number' => '567-890-123456',
                'account_holder' => '주식회사 코드보잇지엑스',
                'account_name' => '퇴직금 적금',
                'account_type' => '적금',
                'balance' => 50000000, // 5천만원
                'currency' => 'KRW',
                'opened_at' => '2024-01-01',
                'last_transaction_at' => '2026-01-10 10:00:00',
                'branch_name' => '강남중앙지점',
                'memo' => '직원 퇴직금 적립',
                'status' => 'active',
                'is_primary' => false,
                'sort_order' => 5,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        // 계좌 데이터 삽입
        $accountIds = [];
        foreach ($accounts as $account) {
            $id = DB::table('bank_accounts')->insertGetId($account);
            $accountIds[$account['account_number']] = $id;
        }

        $this->command->info('계좌 5개 생성 완료');

        // 거래내역 데이터 (국민은행 계좌 기준)
        $transactions = [
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['123-456-789012'],
                'transaction_type' => 'deposit',
                'amount' => 50000000,
                'balance_after' => 450000000,
                'transaction_date' => '2026-01-15',
                'transaction_time' => '14:30:00',
                'description' => '프로젝트 대금 입금',
                'counterparty' => '주식회사 클라이언트A',
                'reference_number' => 'INV-2026-0115',
                'category' => '매출',
                'is_reconciled' => true,
                'reconciled_at' => '2026-01-15 15:00:00',
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['123-456-789012'],
                'transaction_type' => 'withdrawal',
                'amount' => 5000000,
                'balance_after' => 400000000,
                'transaction_date' => '2026-01-14',
                'transaction_time' => '11:20:00',
                'description' => '사무실 임대료',
                'counterparty' => '강남빌딩관리(주)',
                'reference_number' => 'RENT-2026-01',
                'category' => '임대료',
                'is_reconciled' => true,
                'reconciled_at' => '2026-01-14 14:00:00',
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['123-456-789012'],
                'transaction_type' => 'transfer',
                'amount' => 30000000,
                'balance_after' => 405000000,
                'transaction_date' => '2026-01-13',
                'transaction_time' => '09:00:00',
                'description' => '급여계좌 이체',
                'counterparty' => '하나은행 급여계좌',
                'reference_number' => 'TRF-2026-0113',
                'category' => '급여',
                'is_reconciled' => true,
                'reconciled_at' => '2026-01-13 10:00:00',
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['123-456-789012'],
                'transaction_type' => 'deposit',
                'amount' => 25000000,
                'balance_after' => 435000000,
                'transaction_date' => '2026-01-10',
                'transaction_time' => '16:45:00',
                'description' => '유지보수 계약금',
                'counterparty' => '주식회사 클라이언트B',
                'reference_number' => 'MAINT-2026-001',
                'category' => '매출',
                'is_reconciled' => false,
                'reconciled_at' => null,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['123-456-789012'],
                'transaction_type' => 'withdrawal',
                'amount' => 2500000,
                'balance_after' => 410000000,
                'transaction_date' => '2026-01-08',
                'transaction_time' => '14:00:00',
                'description' => '클라우드 서비스 이용료',
                'counterparty' => 'AWS Korea',
                'reference_number' => 'AWS-2026-01',
                'category' => '운영비',
                'is_reconciled' => false,
                'reconciled_at' => null,
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        // 거래내역 삽입
        DB::table('bank_transactions')->insert($transactions);

        $this->command->info('거래내역 5개 생성 완료');

        // 신한은행 거래내역
        $shinhanTransactions = [
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['234-567-890123'],
                'transaction_type' => 'withdrawal',
                'amount' => 1500000,
                'balance_after' => 90000000,
                'transaction_date' => '2026-01-15',
                'transaction_time' => '11:20:00',
                'description' => '법인카드 결제',
                'counterparty' => '신한카드',
                'reference_number' => 'CARD-2026-0115',
                'category' => '카드결제',
                'is_reconciled' => true,
                'reconciled_at' => '2026-01-15 12:00:00',
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'tenant_id' => $tenantId,
                'bank_account_id' => $accountIds['234-567-890123'],
                'transaction_type' => 'deposit',
                'amount' => 10000000,
                'balance_after' => 91500000,
                'transaction_date' => '2026-01-10',
                'transaction_time' => '09:30:00',
                'description' => '카드결제계좌 입금',
                'counterparty' => '국민은행 주거래계좌',
                'reference_number' => 'TRF-2026-0110',
                'category' => '이체',
                'is_reconciled' => true,
                'reconciled_at' => '2026-01-10 10:00:00',
                'created_by' => $userId,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        DB::table('bank_transactions')->insert($shinhanTransactions);

        $this->command->info('신한은행 거래내역 2개 생성 완료');

        $this->command->info('BankAccountSeeder 완료: 계좌 5개, 거래내역 7개');
    }
}
