<?php

namespace App\Domains\Client\Controllers;

use App\Domains\Client\Models\Client;
use App\Domains\Client\Requests\StoreClientRequest;
use App\Domains\Client\Requests\UpdateClientRequest;
use App\Domains\Client\Resources\ClientResource;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class ClientController extends Controller
{
    public function index(): JsonResponse
    {
        return ClientResource::collection(
            Client::query()->with('tenants')->latest()->paginateFromRequest()
        )->response();
    }

    public function store(StoreClientRequest $request): JsonResponse
    {
        return ClientResource::make(
            Client::query()->create($request->validated())->load('tenants')
        )->response()->setStatusCode(201);
    }

    public function show(Client $client): ClientResource
    {
        return ClientResource::make($client->load('tenants'));
    }

    public function update(UpdateClientRequest $request, Client $client): ClientResource
    {
        $client->update($request->validated());

        return ClientResource::make($client->fresh()->load('tenants'));
    }

    public function destroy(Client $client): Response
    {
        $client->delete();

        return response()->noContent();
    }
}
