<?php

namespace App\Http\Controllers\Admin\Rental;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Rental\InsuranceCompany;

class InsuranceCompanyController extends Controller
{
    public function index()
    {
        $companies = InsuranceCompany::latest()->paginate(15);
        return view('admin.rentals.insurances.companies.index', compact('companies'));
    }

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

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'phone' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:255',
            'contact_person' => 'nullable|string|max:255',
            'notes' => 'nullable|string',
            'is_active' => 'boolean'
        ]);

        InsuranceCompany::create($validated);

        return redirect()->route('admin.insurance-companies.index')->with('success', 'Insurance company added successfully.');
    }

    public function edit(InsuranceCompany $company)
    {
        return view('admin.rentals.insurances.companies.edit', compact('company'));
    }

    public function update(Request $request, InsuranceCompany $company)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'phone' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:255',
            'contact_person' => 'nullable|string|max:255',
            'notes' => 'nullable|string',
            'is_active' => 'boolean'
        ]);

        $company->update($validated);

        return redirect()->route('admin.insurance-companies.index')->with('success', 'Insurance company updated successfully.');
    }

    public function destroy(InsuranceCompany $company)
    {
        $company->delete();
        return redirect()->route('admin.insurance-companies.index')->with('success', 'Insurance company deleted successfully.');
    }
}
