<?php

namespace App\Http\Controllers;

use App\Models\Property;
use App\Models\Fractional;
use App\Models\PropertyType;
use Illuminate\Http\Request;

class PropertyController extends Controller
{
    public function index()
    {
        $properties = Property::with(['fractional.project', 'propertyType'])->get();
        return view('properties.index', compact('properties'));
    }

    public function create()
    {
        $fractionals = Fractional::all();
        $propertyTypes = PropertyType::all();
        return view('properties.create', compact('fractionals', 'propertyTypes'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'fractional_id' => 'required|exists:fractionals,id',
            'property_type_id' => 'required|exists:property_types,id',
            'code' => 'required|string|max:50|unique:properties,code',
            'svg_code' => 'nullable|string',
            'name' => 'required|string|max:255',
            'price' => 'required|numeric|min:0',
            'area' => 'required|numeric|min:0',
            'status' => 'required|string|in:available,reserved,sold',
        ]);

        Property::create($request->all());

        return redirect()->route('properties.index')->with('success', 'Propiedad creada correctamente.');
    }

    public function show(Property $property)
    {
        $property->load(['fractional.project', 'propertyType']);
        return view('properties.show', compact('property'));
    }

    public function edit(Property $property)
    {
        $fractionals = Fractional::all();
        $propertyTypes = PropertyType::all();
        return view('properties.edit', compact('property', 'fractionals', 'propertyTypes'));
    }

    public function update(Request $request, Property $property)
    {
        $request->validate([
            'fractional_id' => 'required|exists:fractionals,id',
            'property_type_id' => 'required|exists:property_types,id',
            'code' => 'required|string|max:50|unique:properties,code,' . $property->id,
            'svg_code' => 'nullable|string',
            'name' => 'required|string|max:255',
            'price' => 'nullable|numeric|min:0',
            'area' => 'nullable|numeric|min:0',
            'status' => 'required|string|in:available,reserved,sold',
        ]);

        $property->update($request->all());

        return redirect()->route('properties.index')->with('success', 'Propiedad actualizada correctamente.');
    }

    public function destroy(Property $property)
    {
        $property->delete();
        return redirect()->route('properties.index')->with('success', 'Propiedad eliminada correctamente.');
    }

    public function clone(Property $property)
{
    $newProperty = $property->replicate(); // Duplica todos los atributos menos la clave primaria
    $newProperty->code = $property->code . '-COPY'; // Cambia el código para evitar duplicados
    $newProperty->name = $property->name . ' (Copia)';
    $newProperty->status = 'available'; // o lo que quieras por defecto

    $newProperty->save();

    return redirect()->route('properties.edit', $newProperty)->with('success', 'Propiedad clonada con éxito');
}

}
