<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\TenantStatField\TenantStatFieldStoreRequest;
use App\Http\Requests\TenantStatField\TenantStatFieldUpdateRequest;
use App\Services\TenantStatFieldService;
use Illuminate\Http\Request;

class TenantStatFieldController extends Controller
{
    public function __construct(private TenantStatFieldService $service) {}

    // GET /tenant-stat-fields
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, __('message.tenant_stat_field.fetched'));
    }

    // POST /tenant-stat-fields
    public function store(TenantStatFieldStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.tenant_stat_field.created'));
    }

    // GET /tenant-stat-fields/{id}
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.tenant_stat_field.fetched'));
    }

    // PATCH /tenant-stat-fields/{id}
    public function update(int $id, TenantStatFieldUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.tenant_stat_field.updated'));
    }

    // DELETE /tenant-stat-fields/{id}
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

            return 'success';
        }, __('message.tenant_stat_field.deleted'));
    }

    // POST /tenant-stat-fields/reorder
    public function reorder(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $this->service->reorder($request->input());

            return 'success';
        }, __('message.tenant_stat_field.reordered'));
    }

    // PUT /tenant-stat-fields/bulk-upsert
    public function bulkUpsert(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->bulkUpsert($request->input('items', []));
        }, __('message.tenant_stat_field.bulk_upsert'));
    }
}
