<?php

namespace Database\Seeders;

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

class ProjectSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Get status and type IDs
        $preSalesStatus = DB::table('project_statuses')->where('name', 'Pre-sales')->first();
        $sowApprovalStatus = DB::table('project_statuses')->where('name', 'SOW Approval')->first();
        $estimationStatus = DB::table('project_statuses')->where('name', 'Estimation')->first();
        $inProgressStatus = DB::table('project_statuses')->where('name', 'In Progress')->first();
        $onHoldStatus = DB::table('project_statuses')->where('name', 'On Hold')->first();

        $projectType = DB::table('project_types')->where('name', 'Project')->first();
        $supportType = DB::table('project_types')->where('name', 'Support')->first();

        if (! $preSalesStatus || ! $projectType) {
            $this->command->warn('Required statuses or types not found. Run ProjectStatusSeeder and ProjectTypeSeeder first.');

            return;
        }

        $projects = [
            [
                'id' => Str::uuid()->toString(),
                'code' => 'PROJ-001',
                'title' => 'Website Redesign',
                'status_id' => $preSalesStatus->id, // Pre-sales for transition testing
                'type_id' => $projectType->id,
                'approved_estimate' => null,
                'forecasted_effort' => null,
            ],
            [
                'id' => Str::uuid()->toString(),
                'code' => 'PROJ-002',
                'title' => 'API Integration',
                'status_id' => $estimationStatus->id ?? $preSalesStatus->id,
                'type_id' => $projectType->id,
                'approved_estimate' => null,
                'forecasted_effort' => null,
            ],
            [
                'id' => Str::uuid()->toString(),
                'code' => 'SUP-001',
                'title' => 'Bug Fixes',
                'status_id' => $onHoldStatus->id ?? $preSalesStatus->id,
                'type_id' => $supportType->id,
                'approved_estimate' => 40.00,
                'forecasted_effort' => json_encode(['2024-02' => 20, '2024-03' => 20]),
            ],
            [
                'id' => Str::uuid()->toString(),
                'code' => 'PROJ-003',
                'title' => 'Mobile App Development',
                'status_id' => $inProgressStatus->id ?? $preSalesStatus->id,
                'type_id' => $projectType->id,
                'approved_estimate' => 120.00,
                'forecasted_effort' => json_encode(['2024-02' => 40, '2024-03' => 50, '2024-04' => 30]),
            ],
        ];

        foreach ($projects as $project) {
            DB::table('projects')->updateOrInsert(
                ['code' => $project['code']],
                array_merge($project, [
                    'created_at' => now(),
                    'updated_at' => now(),
                ])
            );
        }

        $this->command->info('Seeded '.count($projects).' projects.');
    }
}
