<?php

namespace App\Services;

use App\Http\Resources\ClientFavoriteProviderResource;
use App\Models\ClientFavoriteProvider;
use App\Models\Provider;
use Illuminate\Database\Eloquent\Collection;

class ClientFavoriteProviderService
{
    public function getByClientId(int $clientId): Collection
    {
        return ClientFavoriteProvider::where('client_favorite_providers.client_id', $clientId)
            ->with('provider.profileMedia')
            ->leftJoin('providers', 'providers.id', '=', 'client_favorite_providers.provider_id')
            ->whereExists(Provider::hasActivePrimaryBankAccount())
            ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
            ->leftJoin('addresses as provider_address', function ($join) {
                $join->on('provider_address.source_id', '=', 'providers.id')
                    ->where('provider_address.source', 'provider')
                    ->whereNull('provider_address.deleted_at');
            })
            ->leftJoin('cities', 'cities.id', '=', 'provider_address.city_id')
            ->select(
                'client_favorite_providers.id',
                'client_favorite_providers.client_id',
                'client_favorite_providers.provider_id',
                'client_favorite_providers.notes',
                'client_favorite_providers.created_at',
                'client_favorite_providers.updated_at',
                'provider_user.name as provider_name',
                'providers.gender',
                'providers.average_rating',
                'providers.daily_price_8h',
                'providers.daily_price_6h',
                'providers.daily_price_4h',
                'providers.daily_price_2h',
                'providers.total_services',
                'cities.name as city_name',
            )
            ->orderBy('client_favorite_providers.created_at', 'desc')
            ->get();
    }

    public function getById(int $id): ClientFavoriteProviderResource
    {
        $favorite = ClientFavoriteProvider::with(['provider.user', 'provider.profileMedia'])->findOrFail($id);

        return new ClientFavoriteProviderResource($favorite);
    }

    public function create(array $data): ClientFavoriteProviderResource
    {
        $favorite = ClientFavoriteProvider::create($data);

        $favorite->load(['provider.user', 'provider.profileMedia']);

        return new ClientFavoriteProviderResource($favorite);
    }

    public function update(int $id, array $data): ClientFavoriteProviderResource
    {
        $favorite = ClientFavoriteProvider::findOrFail($id);

        $favorite->update($data);

        $favorite->load(['provider.user', 'provider.profileMedia']);

        return new ClientFavoriteProviderResource($favorite);
    }

    public function delete(int $id): bool
    {
        $favorite = ClientFavoriteProvider::findOrFail($id);

        return $favorite->delete();
    }

    //

    public function getFavoritedProviderIds(int $clientId): array
    {
        return ClientFavoriteProvider::where('client_id', $clientId)
            ->pluck('provider_id')
            ->toArray();
    }
}
