<?php

namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Http\Request;
use App\Models\Image;

class ProjectController extends Controller
{
    public function index()
    {
        $projects = Project::latest()->paginate(10);
        return view('projects.index', compact('projects'));
    }

    public function create()
    {
        $images = Image::all();
    return view('projects.create', compact('images'));

    }

    public function show(Project $project)
{
    return view('projects.show', compact('project'));
}


    public function store(Request $request)
{
    $validated = $request->validate([
        'name' => 'required|string|max:255',
        'description' => 'nullable|string',
        'location' => 'nullable|string|max:255',
        'image_id' => 'nullable|exists:images,id',
        'company_name' => 'nullable|string|max:255',
        'company_description' => 'nullable|string',
        'company_phone' => 'nullable|string|max:20',
        'company_email' => 'nullable|email|max:255',
        'company_address' => 'nullable|string|max:255',
        'company_logo' => 'nullable|mimes:webp,jpg,jpeg,png,gif,svg|max:2048',
    ]);

    if ($request->hasFile('company_logo')) {
        $path = $request->file('company_logo')->store('logos', 'public');
        $validated['company_logo'] = $path;
    }

    Project::create($validated);

    return redirect()->route('projects.index')->with('success', 'Proyecto creado correctamente.');
}


    public function edit(Project $project)
    {
          $images = Image::all();
    return view('projects.edit', compact('project', 'images'));
    }



    public function update(Request $request, Project $project)
{
    $validated = $request->validate([
        'name' => 'required|string|max:255',
        'description' => 'nullable|string',
        'location' => 'nullable|string|max:255',
        'image_id' => 'nullable|exists:images,id',
        'company_name' => 'nullable|string|max:255',
        'company_description' => 'nullable|string',
        'company_phone' => 'nullable|string|max:20',
        'company_email' => 'nullable|email|max:255',
        'company_address' => 'nullable|string|max:255',
        'company_logo' => 'nullable|mimes:webp,jpg,jpeg,png,gif,svg|max:2048',
    ]);

    if ($request->hasFile('company_logo')) {
        $path = $request->file('company_logo')->store('logos', 'public');
        $validated['company_logo'] = $path;
    }

    $project->update($validated);

    return redirect()->route('projects.index')->with('success', 'Proyecto actualizado correctamente.');
}


    public function destroy(Project $project)
    {
        $project->delete();
        return redirect()->route('projects.index')->with('success', 'Proyecto eliminado.');
    }

    public function public(Project $project)
{
    // Carga la imagen del proyecto y sus secciones con los puntos
    $project->load([
        'image',
        'sections' => function ($q) {
            $q->select('id', 'project_id', 'code', 'description', 'image', 'svg_points');
        },
    ]);

    $sections = $project->sections;

    // Renderiza la vista pública del proyecto
    return view('projects.public', compact('project', 'sections'));
}

}
