<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Services\PeopleServices;
use App\Traits\GetModelsPaginated;
class PeopleController extends Controller
{
    use GetModelsPaginated;

    protected $peopleServices;
    public function __construct(PeopleServices $peopleServices)
    {
        $this->peopleServices = $peopleServices;
    }
    public function index(Request $request)
    {
        return view('admin.people.index');
    }

    public function create(Request $request)
    {
        return view('admin.people.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'first_name' => ['required', 'string', 'max:100'],
            'last_name' => ['required', 'string', 'max:100'],
            'birth_date' => ['nullable', 'date'],
            'gender' => ['nullable', 'in:male,female,other'],
            'phone' => ['nullable', 'string', 'max:30'],
            'email' => ['nullable', 'email', 'max:255'],
            'address' => ['nullable', 'string', 'max:255'],
            'document_type' => ['nullable', 'string', 'max:30'],
            'document_number' => ['nullable', 'string', 'max:50'],
        ]);

        $this->peopleServices->createPeople($validated);

        return redirect()
            ->route('admin_fit.people.index')
            ->with('success', 'Persona creada correctamente.');
    }

    public function peoplePaginatedJson(Request $request)
    {
        return $this->paginateFromService(
            $request,
            fn (int $page, int $length, ?string $searchValue, ?string $status) =>
                $this->peopleServices->getPeopleData($page, $length, $searchValue, $status),
            [
                'model' => 'people',
                'fields' => ['id', 'first_name', 'last_name', 'phone', 'created_at_formatted'],
            ]
        );
    }
}
