<?php

namespace App\Http\Controllers;

use App\Http\Requests\ServicePackageRequest;
use App\Http\Resources\ServicePackageResource;
use App\Services\ServicePackageService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class ServicePackageController extends Controller
{
    public function __construct(
        protected ServicePackageService $service,
    ) {}

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

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

    public function store(ServicePackageRequest $request): JsonResponse
    {
        $validated = $request->validated();

        $item = $this->hasSchedulePayload($validated)
            ? $this->service->createWithSchedules($validated)
            : $this->service->create($validated);

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

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

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

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

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

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

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

    //

    public function myServicePackages(): JsonResponse
    {
        $items = $this->service->getByUserId(Auth::id());

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

    public function storeMine(ServicePackageRequest $request): JsonResponse
    {
        $request->merge(['client_id' => Auth::user()?->client?->id]);

        $item = $this->service->createForUser($request->validated(), Auth::id());

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

    //

    public function providerServicePackages(): JsonResponse
    {
        $items = $this->service->getByProviderUserId(Auth::id());

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

    public function acceptByProvider(int $id): JsonResponse
    {
        try {
            $item = $this->service->acceptByProvider($id, Auth::id());

            return $this->successResponse(payload: new ServicePackageResource($item), message: __('messages.updated'));
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage(), 422);
        }
    }

    public function refuseByProvider(int $id): JsonResponse
    {
        try {
            $item = $this->service->refuseByProvider($id, Auth::id());

            return $this->successResponse(payload: new ServicePackageResource($item), message: __('messages.updated'));
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage(), 422);
        }
    }

    //

    private function hasSchedulePayload(array $data): bool
    {
        return ! empty(data_get($data, 'schedules'))
            || ! empty(data_get($data, 'dates'))
            || ! empty(data_get($data, 'date'));
    }
}
