<?php

namespace App\Http\Livewire\MainPage;

use App\Http\Livewire\Calls\CallCarouselScorer;
use App\Http\Livewire\Calls\ProfileCalls;
use App\Models\Call;
use App\Models\Locations\City;
use App\Models\Locations\Country;
use App\Models\Locations\Division;
use App\Models\Locations\Location;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class CallCardHalf extends Component
{
    public array $calls     = [];
    public bool  $related   = true;
    public bool  $random    = false;
    public int   $rows      = 1;
    public bool  $showScore = false;

    private const UNKNOWN_COUNTRY_ID = 10;

    public function mount(bool $related, bool $random = false, int $rows = 1): void
    {
        $this->related = $related;
        $this->random  = $random;
        $this->rows    = max(1, $rows);

        $carouselCfg = timebank_config('calls.carousel', []);
        $locale      = App::getLocale();

        // --- Resolve active profile and its location ---
        $profile = getActiveProfile();
        if ($profile) {
            \App\Helpers\ProfileAuthorizationHelper::authorize($profile);
        }

        $location          = $profile?->locations ? $profile->locations()->first() : null;
        $profileCityId     = $location ? ($location->city_id     ?? $location->city?->id)     : null;
        $profileDivisionId = $location ? ($location->division_id ?? $location->division?->id) : null;
        $profileCountryId  = $location ? ($location->country_id  ?? $location->country?->id)  : null;

        $locationCityIds     = [];
        $locationDivisionIds = [];
        $locationCountryIds  = [];

        if ($location) {
            if (!$location->division && !$location->city) {
                $country = Location::find($location->id)->country;
                $locationCountryIds = $related
                    ? Country::pluck('id')->toArray()
                    : ($country ? [$country->id] : []);
            } elseif ($location->division && !$location->city) {
                $divisionId = Location::find($location->id)->division->id;
                $locationDivisionIds = $related
                    ? Division::find($divisionId)->parent->divisions->pluck('id')->toArray()
                    : [$divisionId];
            } elseif ($location->city) {
                $cityId = Location::find($location->id)->city->id;
                $locationCityIds = $related
                    ? City::find($cityId)->parent->cities->pluck('id')->toArray()
                    : [$cityId];
            }
        }

        // --- Base query ---
        $query = Call::with([
                'tag.contexts.category.translations',
                'tag.contexts.category.ancestors.translations',
                'translations',
                'location.city.translations',
                'location.country.translations',
                'callable.locations.city.translations',
                'callable.locations.division.translations',
                'callable.locations.country.translations',
                'callable.loveReactant.reactionCounters',
                'loveReactant.reactionCounters',
            ])
            ->whereNull('deleted_at')
            ->where('is_paused', false)
            ->where('is_suppressed', false)
            ->where(fn ($q) => $q->whereNull('till')->orWhere('till', '>=', now()));

        if (!Auth::check() || ($carouselCfg['exclude_non_public'] ?? true)) {
            $query->where('is_public', true);
        }

        if ($profile && ($carouselCfg['exclude_own_calls'] ?? true)) {
            $query->where(function ($q) use ($profile) {
                $q->where('callable_type', '!=', get_class($profile))
                  ->orWhere('callable_id', '!=', $profile->id);
            });
        }

        // --- Locality filter ---
        $includeUnknown  = $carouselCfg['include_unknown_location'] ?? true;
        $includeDivision = $carouselCfg['include_same_division']    ?? true;
        $includeCountry  = $carouselCfg['include_same_country']     ?? true;

        $hasLocalityFilter = $locationCityIds || $locationDivisionIds || $locationCountryIds
            || ($includeDivision && $locationDivisionIds)
            || ($includeCountry  && $locationCountryIds)
            || $includeUnknown;

        if ($hasLocalityFilter) {
            $query->where(function ($q) use (
                $locationCityIds, $locationDivisionIds, $locationCountryIds,
                $includeDivision, $includeCountry, $includeUnknown
            ) {
                if ($locationCityIds) {
                    $q->orWhereHas('location', fn ($lq) => $lq->whereIn('city_id', $locationCityIds));
                }
                if ($includeDivision && $locationDivisionIds) {
                    $q->orWhereHas('location', fn ($lq) => $lq->whereIn('division_id', $locationDivisionIds));
                }
                if ($includeCountry && $locationCountryIds) {
                    $q->orWhereHas('location', fn ($lq) => $lq->whereIn('country_id', $locationCountryIds));
                }
                if ($includeUnknown) {
                    $q->orWhereHas('location', fn ($lq) => $lq->where('country_id', self::UNKNOWN_COUNTRY_ID));
                }
            });
        }

        $isAdmin = getActiveProfileType() === 'Admin';
        $this->showScore = (bool) ($carouselCfg['show_score'] ?? false)
            || ($isAdmin && (bool) ($carouselCfg['show_score_for_admins'] ?? true));

        $limit    = $this->rows * 2;
        $poolSize = $limit * max(1, (int) ($carouselCfg['pool_multiplier'] ?? 5));

        $scorer = new CallCarouselScorer(
            $carouselCfg,
            $profileCityId,
            $profileDivisionId,
            $profileCountryId
        );

        $calls = $query->limit($poolSize)->get();

        $this->calls = $calls->map(function (Call $call) use ($locale, $scorer) {
            $translation = $call->translations->firstWhere('locale', $locale)
                ?? $call->translations->first();

            $tag         = $call->tag;
            $tagContext  = $tag?->contexts->first();
            $tagCategory = $tagContext?->category;
            $tagColor    = $tagCategory?->relatedColor ?? 'gray';
            $tagName     = $tag?->translation?->name ?? $tag?->name;

            $locationStr = null;
            if ($call->location) {
                $loc   = $call->location;
                $parts = [];
                if ($loc->city) {
                    $cityName = optional($loc->city->translations->first())->name;
                    if ($cityName) $parts[] = $cityName;
                }
                if ($loc->country) {
                    if ($loc->country->code === 'XX') {
                        $parts[] = __('Location not specified');
                    } elseif ($loc->country->code) {
                        $parts[] = strtoupper($loc->country->code);
                    }
                }
                $locationStr = $parts ? implode(', ', $parts) : null;
            }

            $tagCategories = [];
            if ($tagCategory) {
                $ancestors = $tagCategory->ancestorsAndSelf()->get()->reverse();
                foreach ($ancestors as $cat) {
                    $catName = $cat->translations->firstWhere('locale', $locale)?->name
                        ?? $cat->translations->first()?->name
                        ?? '';
                    if ($catName) {
                        $tagCategories[] = [
                            'name'  => $catName,
                            'color' => $cat->relatedColor ?? 'gray',
                        ];
                    }
                }
            }

            $score = $scorer->score($call);

            if ($this->random) {
                $score *= (random_int(85, 115) / 100);
            }

            return [
                'id'                => $call->id,
                'model'             => Call::class,
                'title'             => $tagName ?? '',
                'excerpt'           => $translation?->content ?? '',
                'photo'             => $call->callable?->profile_photo_url ?? '',
                'location'          => $locationStr,
                'tag_color'         => $tagColor,
                'tag_categories'    => $tagCategories,
                'callable_name'     => $call->callable?->name ?? '',
                'callable_location' => ProfileCalls::buildCallableLocation($call->callable),
                'till'              => $call->till,
                'expiry_badge_text' => ProfileCalls::buildExpiryBadgeText($call->till),
                'like_count'        => $call->loveReactant?->reactionCounters
                    ->firstWhere('reaction_type_id', 3)?->count ?? 0,
                'score'             => $score,
            ];
        })
        ->sortByDesc('score')
        ->take($limit)
        ->values()
        ->toArray();
    }

    public function render()
    {
        return view('livewire.main-page.call-card-half');
    }
}
