<?php

namespace Database\Seeders;

use App\Models\DamageFinding;
use App\Models\Inspection;
use App\Models\InspectionReport;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $user = User::query()->updateOrCreate(
            ['email' => 'test@example.com'],
            [
                'name' => 'Test User',
                'role' => 'Inspector',
                'password' => Hash::make('password'),
            ]
        );

        if ($user->inspections()->exists()) {
            return;
        }

        $inspectionRows = [
            [
                'property_name' => 'Maple Ridge House',
                'property_address' => '12 Maple Ridge, Bandung',
                'status' => 'completed',
                'ai_confidence' => 98.4,
                'estimated_cost' => 18500000,
                'scheduled_at' => now()->subDays(2),
                'completed_at' => now()->subDay(),
                'report' => ['report_number' => 'RPT-1001', 'status' => 'ready', 'published_at' => now()->subHours(8)],
                'findings' => [
                    ['category' => 'Roof leak', 'severity' => 'high', 'status' => 'review', 'confidence' => 96.5, 'estimated_repair_cost' => 7200000],
                    ['category' => 'Wall crack', 'severity' => 'medium', 'status' => 'confirmed', 'confidence' => 91.2, 'estimated_repair_cost' => 3400000],
                ],
            ],
            [
                'property_name' => 'Northline Apartment',
                'property_address' => '88 Northline Tower, Jakarta',
                'status' => 'review',
                'ai_confidence' => 94.8,
                'estimated_cost' => 9700000,
                'scheduled_at' => now()->subDays(4),
                'completed_at' => now()->subDays(3),
                'report' => ['report_number' => 'RPT-1002', 'status' => 'draft'],
                'findings' => [
                    ['category' => 'Moisture stain', 'severity' => 'medium', 'status' => 'review', 'confidence' => 89.9, 'estimated_repair_cost' => 2600000],
                    ['category' => 'Electrical panel', 'severity' => 'low', 'status' => 'confirmed', 'confidence' => 86.3, 'estimated_repair_cost' => 1200000],
                ],
            ],
            [
                'property_name' => 'Oak Terrace Villa',
                'property_address' => '7 Oak Terrace, Yogyakarta',
                'status' => 'scanning',
                'ai_confidence' => 92.1,
                'estimated_cost' => 4200000,
                'scheduled_at' => now()->subHours(14),
                'completed_at' => null,
                'report' => ['report_number' => 'RPT-1003', 'status' => 'pending'],
                'findings' => [
                    ['category' => 'Drainage slope', 'severity' => 'low', 'status' => 'review', 'confidence' => 82.4, 'estimated_repair_cost' => 900000],
                ],
            ],
            [
                'property_name' => 'Cendana Office',
                'property_address' => '21 Cendana Business Park, Surabaya',
                'status' => 'completed',
                'ai_confidence' => 99.0,
                'estimated_cost' => 23100000,
                'scheduled_at' => now()->subDays(9),
                'completed_at' => now()->subDays(8),
                'report' => ['report_number' => 'RPT-1004', 'status' => 'ready', 'published_at' => now()->subDays(7)],
                'findings' => [
                    ['category' => 'HVAC duct issue', 'severity' => 'high', 'status' => 'confirmed', 'confidence' => 97.0, 'estimated_repair_cost' => 9800000],
                    ['category' => 'Ceiling panel', 'severity' => 'medium', 'status' => 'confirmed', 'confidence' => 93.6, 'estimated_repair_cost' => 4600000],
                    ['category' => 'Window seal', 'severity' => 'low', 'status' => 'dismissed', 'confidence' => 75.1, 'estimated_repair_cost' => 750000],
                ],
            ],
        ];

        foreach ($inspectionRows as $row) {
            $report = $row['report'];
            $findings = $row['findings'];
            unset($row['report'], $row['findings']);

            $inspection = Inspection::query()->create([
                ...$row,
                'user_id' => $user->id,
            ]);

            InspectionReport::query()->create([
                ...$report,
                'inspection_id' => $inspection->id,
                'summary' => 'AI inspection report for '.$inspection->property_name,
            ]);

            foreach ($findings as $finding) {
                DamageFinding::query()->create([
                    ...$finding,
                    'inspection_id' => $inspection->id,
                    'notes' => 'Generated demo finding for dashboard data.',
                ]);
            }
        }
    }
}
