<?php

namespace App\Http\Controllers;

use App\Http\Requests\ReviewRequest;
use App\Http\Resources\ReviewDetailResource;
use App\Http\Resources\ReviewResource;
use App\Services\ReviewService;
use Illuminate\Http\JsonResponse;

class ReviewController extends Controller
{
    public function __construct(
        private ReviewService $service
    ) {}

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

        return $this->successResponse(
            payload: ReviewResource::collection($reviews),
        );
    }

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

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

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

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

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

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

    //

    public function indexByOrigin(string $origin, int $originId): JsonResponse
    {
        $reviews = $this->service->getByOrigin($origin, $originId);

        return $this->successResponse(
            payload: ReviewResource::collection($reviews),
        );
    }

    public function indexBySchedule(int $scheduleId): JsonResponse
    {
        $reviews = $this->service->getByScheduleId($scheduleId);

        return $this->successResponse(
            payload: ReviewResource::collection($reviews),
        );
    }

    public function clientReceived(int $clientId): JsonResponse
    {
        $reviews = $this->service->getByClientReceived($clientId);

        return $this->successResponse(
            payload: ReviewDetailResource::collection($reviews),
        );
    }

    public function clientSent(int $clientId): JsonResponse
    {
        $reviews = $this->service->getByClientSent($clientId);

        return $this->successResponse(
            payload: ReviewDetailResource::collection($reviews),
        );
    }

    public function providerReceived(int $providerId): JsonResponse
    {
        $reviews = $this->service->getByProviderReceived($providerId);

        return $this->successResponse(
            payload: ReviewDetailResource::collection($reviews),
        );
    }

    public function providerSent(int $providerId): JsonResponse
    {
        $reviews = $this->service->getByProviderSent($providerId);

        return $this->successResponse(
            payload: ReviewDetailResource::collection($reviews),
        );
    }
}
