<?php

namespace App\Services;

use App\Repositories\Interfaces\BookingRepositoryInterface;
use App\Repositories\Interfaces\RatingRepositoryInterface;
use App\Repositories\Interfaces\UserRepositoryInterface;
use Exception;

class RatingService
{
    /**
     * @var RatingRepositoryInterface
     */
    protected $ratingRepository;

    /**
     * @var BookingRepositoryInterface
     */
    protected $bookingRepository;

    /**
     * @var UserRepositoryInterface
     */
    protected $userRepository;

    private $response = [
        'success' => true,
        'message' => null
    ];

    public function __construct(
        RatingRepositoryInterface $ratingRepository,
        BookingRepositoryInterface $bookingRepository,
        UserRepositoryInterface $userRepository,
    ) {
        $this->ratingRepository = $ratingRepository;
        $this->bookingRepository = $bookingRepository;
        $this->userRepository = $userRepository;
    }

    /**
     * @param array $payload
     *
     * @return array
     */
    public function submitRating(array $payload, object $tokenInfo): array
    {
        try {
            $verifyBooking = $this->bookingRepository->getByColumn('uuid', $payload['booking_id']);
            if (!$verifyBooking) {
                $this->response['success'] = false;
                $this->response['message'] = "Booking not found";
                return $this->response;
            }

            $rater = $this->userRepository->getByColumn('uuid', $tokenInfo['user_info']['uuid']);

            if (!$rater) {
                $this->response['success'] = false;
                $this->response['message'] = "User not found";
                return $this->response;
            }
            
            if ($rater->id !== $verifyBooking->client_id) {
                $this->response['success'] = false;
                $this->response['message'] = "Invalid Request";
                return $this->response;
            }

            if (! $verifyBooking->consultation_closed_at) {
                $this->response['success'] = false;
                $this->response['message'] = "The consultation must be closed before it can be rated";
                return $this->response;
            }

            $isRatingExist = $this->ratingRepository->getByColumn('booking_id', $verifyBooking->id);

            if ($isRatingExist) {
                $this->response['success'] = false;
                $this->response['message'] = "Rating is already submitted";
                return $this->response;
            }

            $payload['booking_id'] = $verifyBooking->id;
            $payload['professional_id'] = $verifyBooking->professional_id;
            $payload['rate_by'] = $rater->id;
            $payload['remarks'] = $payload['remarks'] ?? '';
            $this->ratingRepository->create($payload);
            $this->response['message'] = "Data created";
            return $this->response;
        } catch (Exception $e) {
            app('log')->error("Error", ['errorMessage' => $e->getMessage()]);
            $this->response['success'] = false;
            $this->response['message'] = "Unable to submit rating";
            $this->response['status'] = 500;
            return $this->response;
        }
    }
}
