<?php

namespace App\Services;

use App\Enums\NotificationTypeEnum;
use App\Models\Address;
use App\Models\CustomSchedule;
use App\Models\CustomScheduleSpeciality;
use App\Models\Provider;
use App\Models\Schedule;
use App\Models\ScheduleProposal;
use App\Models\ScheduleRefuse;
use App\Rules\ScheduleBusinessRules;
use App\Services\NotificationService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class CustomScheduleService
{
    public function __construct(
        private readonly ZipCodeCoordinatesService $zipCodeCoordinatesService,
    ) {}

    public function getAll()
    {
        $custom_schedules = CustomSchedule::with([
            'schedule.client.user',
            'schedule.address',
            'serviceType',
            'specialities.speciality',
        ])
            ->orderBy('id', 'desc')
            ->get();

        return $custom_schedules;
    }

    public function getById($id)
    {
        $customSchedule = CustomSchedule::with([
            'schedule.client.user',
            'schedule.client.profileMedia',
            'schedule.address',
            'serviceType',
            'specialities.speciality',
        ])->find($id);

        return $customSchedule;
    }

    public function create(array $data)
    {
        DB::beginTransaction();

        try {
            $quantity = data_get($data, 'quantity', 1);

            $specialityIds = data_get($data, 'speciality_ids', []);

            $createdCustomSchedules = [];

            for ($i = 0; $i < $quantity; $i++) {
                $scheduleData = [
                    'client_id'     => data_get($data, 'client_id'),
                    'provider_id'   => null,
                    'address_id'    => data_get($data, 'address_id'),
                    'date'          => data_get($data, 'date'),
                    'period_type'   => data_get($data, 'period_type'),
                    'schedule_type' => 'custom',
                    'start_time'    => data_get($data, 'start_time'),
                    'end_time'      => data_get($data, 'end_time'),
                    'status'        => 'pending',
                    'total_amount'  => 0,
                    'code'          => str_pad(random_int(0, 9999), 4, '0', STR_PAD_LEFT),
                    'code_verified' => false,
                ];

                $schedule = Schedule::create($scheduleData);

                $customScheduleData = [
                    'schedule_id'     => $schedule->id,
                    'address_type'    => data_get($data, 'address_type'),
                    'service_type_id' => data_get($data, 'service_type_id'),
                    'description'     => data_get($data, 'description'),
                    'min_price'       => data_get($data, 'min_price'),
                    'max_price'       => data_get($data, 'max_price'),
                    'offers_meal'     => data_get($data, 'offers_meal', false),
                ];

                $customSchedule = CustomSchedule::create($customScheduleData);

                if (! empty($specialityIds)) {
                    foreach ($specialityIds as $specialityId) {
                        CustomScheduleSpeciality::create([
                            'custom_schedule_id' => $customSchedule->id,
                            'speciality_id'      => $specialityId,
                        ]);
                    }
                }

                $createdCustomSchedules[] = $customSchedule->load([
                    'schedule.client.user',
                    'schedule.address',
                    'serviceType',
                    'specialities.speciality',
                ]);
            }

            DB::commit();

            return $createdCustomSchedules;
        } catch (\Exception $e) {
            DB::rollBack();

            Log::error('Erro ao criar agendamento personalizado: '.$e->getMessage());

            throw $e;
        }
    }

    public function update($id, array $data)
    {
        DB::beginTransaction();

        try {
            $customSchedule = CustomSchedule::findOrFail($id);

            $schedule = $customSchedule->schedule;

            $scheduleUpdateData = [];

            if (data_get($data, 'address_id') !== null) {
                $scheduleUpdateData['address_id'] = data_get($data, 'address_id');
            }

            if (data_get($data, 'date') !== null) {
                $scheduleUpdateData['date'] = data_get($data, 'date');
            }

            if (data_get($data, 'period_type') !== null) {
                $scheduleUpdateData['period_type'] = data_get($data, 'period_type');
            }

            if (data_get($data, 'start_time') !== null) {
                $scheduleUpdateData['start_time'] = data_get($data, 'start_time');
            }

            if (data_get($data, 'end_time') !== null) {
                $scheduleUpdateData['end_time'] = data_get($data, 'end_time');
            }

            if (! empty($scheduleUpdateData)) {
                $schedule->update($scheduleUpdateData);
            }

            $customScheduleUpdateData = [];

            if (data_get($data, 'address_type') !== null) {
                $customScheduleUpdateData['address_type'] = data_get($data, 'address_type');
            }

            if (data_get($data, 'service_type_id') !== null) {
                $customScheduleUpdateData['service_type_id'] = data_get($data, 'service_type_id');
            }

            if (data_get($data, 'description') !== null) {
                $customScheduleUpdateData['description'] = data_get($data, 'description');
            }

            if (data_get($data, 'min_price') !== null) {
                $customScheduleUpdateData['min_price'] = data_get($data, 'min_price');
            }

            if (data_get($data, 'max_price') !== null) {
                $customScheduleUpdateData['max_price'] = data_get($data, 'max_price');
            }

            if (data_get($data, 'offers_meal') !== null) {
                $customScheduleUpdateData['offers_meal'] = data_get($data, 'offers_meal');
            }

            if (! empty($customScheduleUpdateData)) {
                $customSchedule->update($customScheduleUpdateData);
            }

            if (data_get($data, 'speciality_ids') !== null) {
                $custom_schedule = CustomScheduleSpeciality::where('custom_schedule_id', $customSchedule->id);

                $custom_schedule->delete();

                foreach (data_get($data, 'speciality_ids') as $specialityId) {
                    CustomScheduleSpeciality::create([
                        'custom_schedule_id' => $customSchedule->id,
                        'speciality_id'      => $specialityId,
                    ]);
                }
            }

            DB::commit();

            return $customSchedule->fresh([
                'schedule.client.user',
                'schedule.address',
                'serviceType',
                'specialities.speciality',
            ]);
        } catch (\Exception $e) {
            DB::rollBack();

            Log::error('Erro ao atualizar agendamento personalizado: '.$e->getMessage());

            throw $e;
        }
    }

    public function delete($id)
    {
        DB::beginTransaction();

        try {
            $customSchedule = CustomSchedule::findOrFail($id);
            $schedule = $customSchedule->schedule;

            CustomScheduleSpeciality::where('custom_schedule_id', $customSchedule->id)->delete();

            $customSchedule->delete();

            $schedule->delete();

            DB::commit();

            return $customSchedule;
        } catch (\Exception $e) {
            DB::rollBack();

            Log::error('Erro ao excluir agendamento personalizado: '.$e->getMessage());

            throw $e;
        }
    }

    //

    public function getAvailableOpportunities($providerId)
    {
        $provider = Provider::find($providerId);

        $providerAddress = Address::where('source', 'provider')
            ->where('source_id', $providerId)
            ->orderBy('is_primary', 'desc')
            ->first();

        $opportunities = Schedule::with([
            'client.user',
            'client.profileMedia',
            'address',
            'customSchedule.serviceType',
            'customSchedule.specialities',
        ])
            ->leftJoin('schedule_refuses', function ($join) use ($providerId) {
                $join->on('schedules.id', '=', 'schedule_refuses.schedule_id')
                    ->where('schedule_refuses.provider_id', $providerId);
            })
            ->whereNull('schedule_refuses.id')
            ->where('schedules.schedule_type', 'custom')
            ->where('schedules.status', 'pending')
            ->whereNull('schedules.provider_id')
            ->whereDate('schedules.date', '>=', now()->toDateString())
            ->select(
                'schedules.id',
                'schedules.client_id',
                'schedules.address_id',
                'schedules.date',
                'schedules.period_type',
                'schedules.start_time',
                'schedules.end_time',
                'schedules.total_amount',

                DB::raw("
                    CASE
                        WHEN schedules.period_type = '2' THEN {$provider->daily_price_2h}
                        WHEN schedules.period_type = '4' THEN {$provider->daily_price_4h}
                        WHEN schedules.period_type = '6' THEN {$provider->daily_price_6h}
                        WHEN schedules.period_type = '8' THEN {$provider->daily_price_8h}
                        ELSE 0
                    END AS total_amount
                "),
            )
            ->get();

        $availableOpportunities = $opportunities->filter(function ($opportunity) use ($providerId) {
            try {
                return $this->checkProviderAvailability($providerId, $opportunity);
            } catch (\Exception $e) {
                return false;
            }
        });

        $availableOpportunities->each(function ($opportunity) use ($providerAddress) {
            $opportunity->distance_km = $this->zipCodeCoordinatesService->calculateDistance(
                $providerAddress?->latitude !== null ? (float) $providerAddress->latitude : null,
                $providerAddress?->longitude !== null ? (float) $providerAddress->longitude : null,
                $providerAddress?->zip_code,
                $opportunity->address?->latitude !== null  ? (float) $opportunity->address->latitude  : null,
                $opportunity->address?->longitude !== null ? (float) $opportunity->address->longitude : null,
                $opportunity->address?->zip_code,
            );

            $photoPath = $opportunity->client->profileMedia?->path;

            $opportunity->customer_photo = $photoPath
                ? Storage::temporaryUrl($photoPath, now()->addMinutes(60))
                : null;
        });

        return $availableOpportunities->values();
    }

    public function getOpportunityProposals($scheduleId)
    {
        return ScheduleProposal::with(['provider.user'])
            ->where('schedule_id', $scheduleId)
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getProvidersProposalsAndOpportunities($providerId)
    {
        $proposals = $this->getProviderProposals($providerId);

        $opportunities = $this->formatCustomSchedules($this->getAvailableOpportunities($providerId));

        return [
            'proposals'     => $proposals,
            'opportunities' => $opportunities,
        ];
    }

    public function getProviderProposals($providerId)
    {
        return ScheduleProposal::with([
            'schedule.client.user',
            'schedule.address',
            'schedule.address.city',
            'schedule.address.state',
            'schedule.customSchedule.serviceType',
            'schedule.customSchedule.specialities',
            'schedule.provider.user',
        ])
            ->where('provider_id', $providerId)
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getSchedulesCustomGroupedByClient()
    {
        $schedules = Schedule::with(['client.user', 'provider.user', 'address', 'customSchedule.serviceType', 'customSchedule.specialities', 'reviews.reviewsImprovements.improvementType'])
            ->orderBy('id', 'desc')
            ->where('schedule_type', 'custom')
            ->get();

        $grouped = $this->formatCustomSchedules($schedules);

        return $grouped;
    }

    //

    public function proposeOpportunity($scheduleId, $providerId)
    {
        $schedule = Schedule::findOrFail($scheduleId);

        if ($schedule->provider_id) {
            throw new \Exception(__('validation.custom.opportunity.already_assigned'));
        }

        $existingProposal = ScheduleProposal::where('schedule_id', $scheduleId)
            ->where('provider_id', $providerId)
            ->first();

        if ($existingProposal) {
            throw new \Exception(__('validation.custom.opportunity.proposal_already_sent'));
        }

        $wasRefused = ScheduleRefuse::where('schedule_id', $scheduleId)
            ->where('provider_id', $providerId)
            ->exists();

        if ($wasRefused) {
            throw new \Exception(__('validation.custom.opportunity.provider_refused'));
        }

        $this->checkProviderAvailability($providerId, $schedule);

        $provider = Provider::with([
            'user'
        ])->findOrFail($providerId);

        $schedule->load([
            'client.user'
        ]);

        $notificationService = app(NotificationService::class);

        $notificationService->create([
            'title'       => __('notifications.new_proposal_title'),
            'description' => __('notifications.new_proposal_description', ['provider' => $provider->user->name]),
            'origin'      => 'schedule',
            'origin_id'   => $schedule->id,
            'type'        => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_NEW_SOLICITATION->value,
            'user_id'     => $schedule->client->user_id,
        ]);

        return ScheduleProposal::create([
            'schedule_id' => $scheduleId,
            'provider_id' => $providerId,
        ]);
    }

    public function refuseOpportunity($scheduleId, $providerId)
    {

        $schedule = Schedule::with(['client.user'])->findOrFail($scheduleId);

        $provider = Provider::with(['user'])->findOrFail($providerId);

        $schedule_refuse = ScheduleRefuse::create([
            'schedule_id' => $scheduleId,
            'provider_id' => $providerId,
        ]);

        $notificationService = app(NotificationService::class);

        $notificationService->create([
            'title'       => __('notifications.opportunity_refused_title'),
            'description' => __('notifications.opportunity_refused_description', ['provider' => $provider->user->name]),
            'origin'      => 'schedule',
            'origin_id'   => $scheduleId,
            'type'        => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
            'user_id'     => $schedule->client->user_id,
        ]);

        return $schedule_refuse;
    }

    //

    public function acceptProposal($proposalId)
    {
        return DB::transaction(function () use ($proposalId) {
            $proposal = ScheduleProposal::findOrFail($proposalId);
            $schedule = $proposal->schedule;

            if ($schedule->provider_id) {
                throw new \Exception(__('validation.custom.opportunity.already_assigned'));
            }

            $provider = Provider::find($proposal->provider_id);

            switch ($schedule->period_type) {
                case '8':
                    $baseAmount = $provider->daily_price_8h;
                    break;
                case '6':
                    $baseAmount = $provider->daily_price_6h;
                    break;
                case '4':
                    $baseAmount = $provider->daily_price_4h;
                    break;
                case '2':
                    $baseAmount = $provider->daily_price_2h;
                    break;
                default:
                    throw new \Exception(__('messages.invalid_schedule_period'));
            }

            $schedule->total_amount = $baseAmount;

            $schedule->save();

            $schedule->update([
                'provider_id' => $proposal->provider_id,
            ]);

            $schedule->refresh();

            $schedule->load(['provider.user', 'client.user']);

            $notificationService = app(NotificationService::class);

            $notificationService->create([
                'title'       => __('notifications.proposal_accepted_title'),
                'description' => __('notifications.proposal_accepted_description'),
                'origin'      => 'schedule',
                'origin_id'   => $schedule->id,
                'type'        => NotificationTypeEnum::SCHEDULE_PROVIDER_CLIENT_PROPOSAL_ACCEPTED->value,
                'user_id'     => $provider->user_id,
            ]);

            app(ScheduleService::class)->updateStatus($schedule->id, 'accepted');

            ScheduleProposal::where('schedule_id', $schedule->id)
                ->where('id', '!=', $proposalId)
                ->delete();

            return $schedule->fresh(['provider.user']);
        });
    }

    public function refuseProposal($proposalId)
    {
        return DB::transaction(function () use ($proposalId) {
            $proposal = ScheduleProposal::findOrFail($proposalId);

            ScheduleRefuse::create([
                'schedule_id' => $proposal->schedule_id,
                'provider_id' => $proposal->provider_id,
            ]);

            $notificationService = app(NotificationService::class);

            $notificationService->create([
                'title'       => __('notifications.proposal_refused_title'),
                'description' => __('notifications.proposal_refused_description'),
                'origin'      => 'schedule',
                'origin_id'   => $proposal->schedule_id,
                'type'        => NotificationTypeEnum::SCHEDULE_CLIENT_PROVIDER_REFUSED->value,
                'user_id'     => $proposal->provider->user_id,
            ]);


            $proposal->delete();

            return true;
        });
    }

    //

    public function formatCustomSchedules($schedules)
    {
        $grouped = $schedules->groupBy('client_id')->map(function ($clientSchedules) {
            $firstSchedule = $clientSchedules->first();

            $clientPhotoPath = $firstSchedule->client->profileMedia?->path;

            return [
                'client_id'      => $firstSchedule->client_id,
                'client_name'    => $firstSchedule->client->user->name ?? 'N/A',

                'customer_photo' => $clientPhotoPath
                    ? Storage::temporaryUrl($clientPhotoPath, now()->addMinutes(60))
                    : null,

                'schedules' => $clientSchedules->map(function ($schedule) {
                    $customSchedule = $schedule->customSchedule;

                    return [
                        'id'            => $schedule->id,
                        'date'          => $schedule->date ? Carbon::parse($schedule->date)->format('d/m/Y') : null,
                        'start_time'    => $schedule->start_time,
                        'end_time'      => $schedule->end_time,
                        'period_type'   => $schedule->period_type,
                        'status'        => $schedule->status,
                        'total_amount'  => $schedule->total_amount,
                        'code'          => $schedule->code,
                        'code_verified' => $schedule->code_verified,
                        'provider_id'   => $schedule->provider_id,
                        'client_id'     => $schedule->client_id,
                        'provider_name' => $schedule->provider?->user->name ?? 'N/A',

                        'address' => $schedule->address ? [
                            'id'         => $schedule->address->id,
                            'address'    => $schedule->address->address,
                            'complement' => $schedule->address->complement,
                            'zip_code'   => $schedule->address->zip_code,
                            'city'       => $schedule->address->city->name        ?? '',
                            'state'      => $schedule->address->city->state->name ?? '',
                        ] : null,

                        'client_name' => $schedule->client->user->name ?? 'N/A',

                        'custom_schedule' => $customSchedule ? [
                            'id'                => $customSchedule->id,
                            'address_type'      => $customSchedule->address_type,
                            'service_type_id'   => $customSchedule->service_type_id,
                            'service_type_name' => $customSchedule->serviceType?->description ?? 'N/A',
                            'description'       => $customSchedule->description,
                            'min_price'         => $customSchedule->min_price,
                            'max_price'         => $customSchedule->max_price,
                            'offers_meal'       => $customSchedule->offers_meal,

                            'specialities' => $customSchedule->specialities->map(function ($speciality) {
                                return [
                                    'id'          => $speciality->id,
                                    'description' => $speciality->description,
                                ];
                            })->values(),
                        ] : null,

                        'reviews' => $schedule->reviews->map(function ($review) {
                            return [
                                'id'           => $review->id,
                                'stars'        => $review->stars,
                                'comment'      => $review->comment,
                                'origin'       => $review->origin,
                                'origin_id'    => $review->origin_id,
                                'created_at'   => Carbon::parse($review->created_at)->format('Y-m-d H:i'),
                                'updated_at'   => Carbon::parse($review->updated_at)->format('Y-m-d H:i'),

                                'improvements' => $review->reviewsImprovements->map(function ($ri) {
                                    return [
                                        'id'                    => $ri->id,
                                        'improvement_type_id'   => $ri->improvement_type_id,
                                        'improvement_type_name' => $ri->improvementType ? $ri->improvementType->description : null,
                                    ];
                                })->values(),
                            ];
                        }),
                    ];
                })->values(),
            ];
        })->sortBy('id')->values();

        return $grouped;
    }

    public function verifyScheduleCode($scheduleId, $code)
    {
        $schedule = Schedule::findOrFail($scheduleId);

        if ($schedule->code_verified) {
            throw new \Exception(__('validation.custom.opportunity.code_already_verified'));
        }

        if ($schedule->code !== $code) {
            throw new \Exception(__('validation.custom.opportunity.invalid_code'));
        }

        $schedule->update([
            'code_verified' => true,
        ]);

        return $schedule;
    }

    //

    private function checkProviderAvailability($providerId, $schedule)
    {
        $client_id   = $schedule->client_id;
        $provider_id = $providerId;

        $date = Carbon::parse($schedule->date);

        $dayOfWeek   = $date->dayOfWeek; // 0-6
        $startTime   = $schedule->start_time;
        $endTime     = $schedule->end_time;
        $date_ymd    = $date->format('Y-m-d');
        $period      = $startTime < '13:00:00' ? 'morning' : 'afternoon';
        $period_type = $schedule->period_type; // 2,4,6,8

        // bloqueio 2 schedules por semana para o mesmo client e provider

        ScheduleBusinessRules::validateWeeklyScheduleLimit(
            $client_id,
            $provider_id,
            $date_ymd
        );

        // bloqueio provider trabalha no dia/periodo

        ScheduleBusinessRules::validateWorkingDay(
            $provider_id,
            $dayOfWeek,
            $period
        );

        // bloqueio provider tem blockedday para dia/hora

        ScheduleBusinessRules::validateBlockedDay(
            $provider_id,
            $date_ymd,
            $startTime,
            $endTime
        );

        // bloqueio daily_price do provider esta fora do range min_price e max_price

        ScheduleBusinessRules::validatePricePeriod(
            $provider_id,
            $schedule->customSchedule->min_price,
            $schedule->customSchedule->max_price,
            $period_type
        );

        // bloqueio provider tem outro agendamento para dia/hora

        ScheduleBusinessRules::validateConflictingSchedule(
            $provider_id,
            $date_ymd,
            $startTime,
            $endTime
        );

        // bloqueio provider tem outra proposta para o mesmo agendamento

        ScheduleBusinessRules::validateConflictingSameProposal(
            $provider_id,
            $schedule->id
        );

        // bloqueio provider tem outra proposta na mesma data

        ScheduleBusinessRules::validateConflictingProposalSameDate(
            $provider_id,
            $date_ymd,
            $startTime,
            $endTime,
            $schedule->id
        );

        // bloqueio caso o client tenha bloqueado o provider

        ScheduleBusinessRules::validateClientNotBlockedByProvider(
            $client_id,
            $provider_id
        );

        // bloqueio caso o provider tenha bloqueado o client

        ScheduleBusinessRules::validateProviderNotBlockedByClient(
            $client_id,
            $provider_id
        );

        return true;
    }
}
