<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Project;
use App\Models\ComplianceRequirement;
use App\Models\ProjectComplianceRequirement;

class AssignComplianceToProjectsSeeder extends Seeder
{
    /**
     * Assign all active compliance checklist items to every existing project.
     * Status defaults to 'not_done' so the project team can fill them in.
     */
    public function run(): void
    {
        $requirements = ComplianceRequirement::where('is_active', true)
            ->orderBy('sort_order')
            ->get();

        if ($requirements->isEmpty()) {
            $this->command?->warn('No active compliance requirements found. Run ComplianceChecklistSeeder first.');
            return;
        }

        $projects = Project::all();

        foreach ($projects as $project) {
            foreach ($requirements as $requirement) {
                ProjectComplianceRequirement::firstOrCreate(
                    [
                        'project_id' => $project->id,
                        'compliance_requirement_id' => $requirement->id,
                    ],
                    [
                        'status' => 'not_done',
                        'value' => null,
                        'notes' => null,
                    ]
                );
            }
        }

        $this->command?->info("Assigned {$requirements->count()} checklist items to {$projects->count()} projects.");
    }
}
