<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProviderRequest;
use App\Http\Requests\RegisterProviderRequest;
use App\Http\Resources\AuthResource;
use App\Http\Resources\ProviderResource;
use App\Services\ProviderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProviderController extends Controller
{
    public function __construct(protected ProviderService $service) {}

    public function index(): JsonResponse
    {
        $items = $this->service->getAll();

        return $this->successResponse(
            payload: ProviderResource::collection($items),
        );
    }

    public function store(ProviderRequest $request): JsonResponse
    {
        $item = $this->service->create($request->validated());

        return $this->successResponse(
            payload: new ProviderResource($item),
            message: __('messages.created'),
            code:    201,
        );
    }

    public function show(int $id): JsonResponse
    {
        $item = $this->service->findById($id);

        return $this->successResponse(payload: new ProviderResource($item));
    }

    public function update(ProviderRequest $request, int $id): JsonResponse
    {
        $item = $this->service->update($id, $request->validated());

        return $this->successResponse(
            payload: new ProviderResource($item),
            message: __('messages.updated'),
        );
    }

    public function destroy(int $id): JsonResponse
    {
        $this->service->delete($id);

        return $this->successResponse(
            message: __('messages.deleted'),
            code:    204,
        );
    }

    //

    public function pending(Request $request): JsonResponse
    {
        $page    = $request->integer('page', 1);
        $perPage = $request->integer('per_page', 10);

        $paginated = $this->service->getPending($page, $perPage);

        return $this->successResponse(payload: [
            'data'  => ProviderResource::collection($paginated->items()),
            'total' => $paginated->total(),
            'from'  => $paginated->firstItem() ?? 0,
            'to'    => $paginated->lastItem()  ?? 0,
        ]);
    }

    public function approve(int $id): JsonResponse
    {
        $item = $this->service->approve($id);

        return $this->successResponse(
            payload: new ProviderResource($item),
            message: __('messages.provider_approved'),
        );
    }

    public function reject(int $id): JsonResponse
    {
        $item = $this->service->reject($id);

        return $this->successResponse(
            payload: new ProviderResource($item),
            message: __('messages.provider_rejected'),
        );
    }

    //

    public function register(RegisterProviderRequest $request): JsonResponse
    {
        $result = $this->service->register($request->validated());
        if (! $result) {
            return $this->errorResponse(message: __('auth.failed'), code: 401);
        }

        return $this->successResponse(
            payload: new AuthResource([
                ...data_get($result, 'payload'),
                'refresh_token' => data_get($result, 'refreshToken'),
            ]),
            message: __('messages.provider_registered_pending'),
            code: 201,
        );
    }

    public function dailyPriceMin(): JsonResponse
    {
        return $this->successResponse(payload: [
            'daily_price_min' => (float) config('provider.daily_price_min'),
        ]);
    }
}
