<?php

namespace App\Http\Controllers;

use App\Models\Company;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    public function index()
    {
        $companies = Company::all();
        return view('companies.index', compact('companies'));
    }

    public function create()
    {
        return view('companies.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'nombre' => 'required|string|max:255',
            'rfc' => 'required|string|max:13',
            'email' => 'required|email|unique:companies,email',
            'plan' => 'required',
            'logo' => 'nullable|image|max:2048',
        ]);

        $data = $request->only([
            'nombre',
            'rfc',
            'email',
            'plan'
        ]);

        // Normalizamos el plan para que nunca mande "basico"
        $data['plan'] = match ($request->plan) {
            'basico' => 'basic',
            default => $request->plan,
        };

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

        // 👉 IMPORTANTE: guarda en variable para auditar
        $company = Company::create($data);

        // ✅ REGISTRO DE AUDITORÍA (CREACIÓN)
        audit_log(
            'created',
            'companies',
            $company,
            null,
            $company->toArray()
        );

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


    public function edit(Company $company)
    {
        return view('companies.edit', compact('company'));
    }
public function update(Request $request, Company $company)
{
    $request->validate([
        'nombre' => 'required|string|max:255',
        'rfc' => 'required|string|max:13',
        'email' => 'required|email|unique:companies,email,' . $company->id,
        'plan' => 'required',
        'activo' => 'required|boolean',
        'logo' => 'nullable|image|max:2048',
    ]);

    // 👉 Capturamos estado ANTERIOR para auditoría
    $oldData = $company->toArray();

    $data = $request->only([
        'nombre',
        'rfc',
        'email',
        'plan',
        'activo'
    ]);

    // Normalizamos plan
    $data['plan'] = match ($request->plan) {
        'basico' => 'basic',
        default => $request->plan,
    };

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

    $company->update($data);

    // 👉 Capturamos estado NUEVO
    $newData = $company->fresh()->toArray();

    // ✅ AUDITORÍA DE ACTUALIZACIÓN
    audit_log(
        'updated',
        'companies',
        $company,
        $oldData,
        $newData
    );

    return redirect()->route('companies.index')
        ->with('success', 'Empresa actualizada correctamente.');
}
public function destroy(Company $company)
{
    $deletedData = $company->toArray();

    $company->delete();

    // ✅ AUDITORÍA DE ELIMINACIÓN
    audit_log(
        'deleted',
        'companies',
        $company,
        $deletedData,
        null
    );

    return redirect()->route('companies.index')
        ->with('success', 'Empresa eliminada.');
}
}
