<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Project;
use App\Models\Product;
use App\Models\Customer;
use App\Models\Contract;
use App\Models\PaymentTemplate;
use App\Models\PaymentSchedule;
use App\Models\PaymentScheduleItem;
use App\Enums\ProductType;
use App\Enums\PaymentType;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;

class TestDataSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Tạo Tài khoản Admin
        User::updateOrCreate(
            ['email' => 'admin@phuongtc.com'],
            [
                'name' => 'chanphuong',
                'password' => Hash::make('1Qazxsw2@!321'),
            ]
        );

        // 2. Tạo Dự án
        $project = Project::updateOrCreate(
            ['code' => 'STH03'],
            [
                'name' => 'Khu đô thị Mỹ Gia - Gói 3',
                'type' => 'Khu đô thị'
            ]
        );

        // 3. Tạo Mẫu thanh toán
        $template = PaymentTemplate::create([
            'project_id' => $project->id,
            'name' => 'Mẫu chuẩn Đất nền 30-40-30'
        ]);

        $project->update(['payment_template_id' => $template->id]);

        // 4. Tạo các đợt mẫu
        PaymentScheduleItem::create([
            'template_id' => $template->id,
            'installment_no' => 1,
            'type' => PaymentType::MONG,
            'percentage' => 30,
            'days_after_signing' => 0,
        ]);
        PaymentScheduleItem::create([
            'template_id' => $template->id,
            'installment_no' => 2,
            'type' => PaymentType::THAN,
            'percentage' => 40,
            'days_after_previous' => 60,
        ]);
        PaymentScheduleItem::create([
            'template_id' => $template->id,
            'installment_no' => 3,
            'type' => PaymentType::OTHER,
            'percentage' => 30,
            'days_after_previous' => 90,
        ]);

        // 5. Tạo Sản phẩm
        Product::create([
            'project_id' => $project->id,
            'product_type' => ProductType::LAND,
            'code' => 'STH03.01',
            'area' => 100,
            'price_per_unit' => 25000000,
            'total_price' => 2500000000,
            'status' => 'Đang mở bán',
        ]);

        $productSold = Product::create([
            'project_id' => $project->id,
            'product_type' => ProductType::LAND,
            'code' => 'STH03.02',
            'area' => 100,
            'price_per_unit' => 25000000,
            'total_price' => 2500000000,
            'status' => 'Đã bán',
        ]);

        // 6. Khách hàng & Hợp đồng chuyển nhượng
        // Sử dụng cột cmnd_cccd thay cho id_number
        $customerA = Customer::create(['full_name' => 'Nguyễn Văn A', 'phone' => '0901234567', 'cmnd_cccd' => '123456789']);
        $customerB = Customer::create(['full_name' => 'Trần Thị B', 'phone' => '0907654321', 'cmnd_cccd' => '987654321']);

        // F1
        $contractF1 = Contract::create([
            'product_id' => $productSold->id,
            'contract_number' => 'HĐMB-STH03.02-F1',
            'signing_date' => Carbon::now()->subMonths(6),
            'total_value' => 2500000000,
            'transfer_order' => 1,
            'status' => 'Đã thanh lý (Chuyển nhượng)',
        ]);
        $contractF1->customers()->attach($customerA->id);

        // F2
        $contractF2 = Contract::create([
            'product_id' => $productSold->id,
            'contract_number' => 'HĐMB-STH03.02-F2',
            'signing_date' => Carbon::now()->subDays(10),
            'total_value' => 2600000000,
            'transfer_order' => 2,
            'status' => 'Đang hiệu lực',
        ]);
        $contractF2->customers()->attach($customerB->id);

        $schedule = PaymentSchedule::create([
            'contract_id' => $contractF2->id,
            'template_id' => $template->id,
        ]);

        PaymentScheduleItem::create([
            'schedule_id' => $schedule->id,
            'installment_no' => 1,
            'type' => PaymentType::MONG,
            'percentage' => 30,
            'amount' => 780000000,
            'due_date' => Carbon::now()->subDays(10),
        ]);
    }
}
