<?php

namespace Database\Seeders;

use App\Models\Feature;
use App\Models\Inspection;
use App\Models\InspectionTemplate;
use App\Models\Layer;
use App\Models\Phase;
use App\Models\Project;
use App\Models\User;
use Illuminate\Database\Seeder;

class ProjectExampleSeeder extends Seeder
{
    public function run()
    {
        // Obtener usuario admin
        $admin = User::where('email', 'admin@email.com')->first();
        if (! $admin) {
            $admin = User::first();
        }

        // Crear proyecto
        $project = Project::create([
            'name' => 'Edificio Central',
            'address' => 'Av. Principal 123, Ciudad',
            'lat' => 40.416775,
            'lng' => -3.703790,
            'start_date' => now(),
            'end_date_estimated' => now()->addMonths(12),
            'status' => 'in_progress',
            'created_by' => $admin->id,
        ]);

        $project->users()->attach($admin->id, ['role_in_project' => 'supervisor']);

        // Crear fases
        $phases = [
            ['name' => 'Cimientos', 'order' => 1, 'color' => '#ef4444'],
            ['name' => 'Estructura', 'order' => 2, 'color' => '#3b82f6'],
            ['name' => 'Instalaciones', 'order' => 3, 'color' => '#10b981'],
            ['name' => 'Acabados', 'order' => 4, 'color' => '#f59e0b'],
        ];

        foreach ($phases as $phaseData) {
            Phase::create(array_merge($phaseData, ['project_id' => $project->id, 'progress_percent' => 0]));
        }

        $phaseCimientos = Phase::where('project_id', $project->id)->where('name', 'Cimientos')->first();

        // Crear templates de inspección
        $templateHormigon = InspectionTemplate::create([
            'project_id' => $project->id,
            'name' => 'Control de hormigón',
            'fields' => [
                ['name' => 'progress', 'label' => 'Progreso de vertido', 'type' => 'percentage', 'required' => true],
                ['name' => 'calidad', 'label' => 'Calidad del hormigón', 'type' => 'select', 'options' => 'Excelente,Bueno,Regular,Deficiente', 'required' => true],
                ['name' => 'observaciones', 'label' => 'Observaciones', 'type' => 'textarea', 'required' => false],
            ],
        ]);

        $templateAcero = InspectionTemplate::create([
            'project_id' => $project->id,
            'name' => 'Control de acero',
            'fields' => [
                ['name' => 'progress', 'label' => '% de acero colocado', 'type' => 'percentage', 'required' => true],
                ['name' => 'diametros', 'label' => 'Diámetros verificados', 'type' => 'text', 'required' => false],
            ],
        ]);

        // Crear capa (sin geojson_data)
        $layer = Layer::create([
            'project_id' => $project->id,
            'phase_id' => $phaseCimientos->id,
            'name' => 'Plano de cimientos',
            'original_file' => null,
            'uploaded_by' => $admin->id,
        ]);

        // Crear features (elementos geográficos)
        $feature1 = Feature::create([
            'layer_id' => $layer->id,
            'name' => 'Zapata A1',
            'geometry' => [
                'type' => 'Polygon',
                'coordinates' => [[
                    [-3.705, 40.415],
                    [-3.702, 40.415],
                    [-3.702, 40.418],
                    [-3.705, 40.418],
                    [-3.705, 40.415],
                ]],
            ],
            'properties' => ['description' => 'Zapata esquina noroeste'],
            'template_id' => $templateHormigon->id,
            'progress' => 45,
            'responsible' => 'Ing. Gómez',
        ]);

        $feature2 = Feature::create([
            'layer_id' => $layer->id,
            'name' => 'Zapata B1',
            'geometry' => [
                'type' => 'Polygon',
                'coordinates' => [[
                    [-3.700, 40.415],
                    [-3.697, 40.415],
                    [-3.697, 40.418],
                    [-3.700, 40.418],
                    [-3.700, 40.415],
                ]],
            ],
            'properties' => ['description' => 'Zapata lado este'],
            'template_id' => $templateAcero->id,
            'progress' => 80,
            'responsible' => 'Arq. Martínez',
        ]);

        Feature::create([
            'layer_id' => $layer->id,
            'name' => 'Punto de control topográfico',
            'geometry' => [
                'type' => 'Point',
                'coordinates' => [-3.703, 40.4165],
            ],
            'properties' => ['tipo' => 'estación total'],
            'template_id' => null,
            'progress' => 100,
            'responsible' => 'Topógrafo Pérez',
        ]);

        // (Opcional) Crear una inspección de ejemplo
        Inspection::create([
            'project_id' => $project->id,
            'layer_id' => $layer->id,
            'feature_id' => $feature1->id,
            'template_id' => $templateHormigon->id,
            'user_id' => $admin->id,
            'data' => [
                'progress' => 45,
                'calidad' => 'Bueno',
                'observaciones' => 'Vertido completado en un 45%, sin fisuras aparentes.',
            ],
        ]);
    }
}
