<?php

namespace App\Libraries;

use Modules\Agent\Models\AgentModel;
use Modules\Ticket\Models\JourneylistModel;
use Modules\User\Models\UserDetailModel;
use Modules\User\Models\UserModel;

class BookingPassengerSupport
{
    public const STAFF_MOBILE_MESSAGE = 'Passenger mobile cannot be an agent or staff account. Please enter the passenger\'s own phone number.';

    public const NON_PASSENGER_ROLE_MESSAGE = 'Passenger account must be a customer profile, not an agent or staff login.';

    public static function staffMobileError(UserModel $userModel, ?string $loginMobile): ?string
    {
        $mobile = trim((string) $loginMobile);
        if ($mobile === '') {
            return null;
        }

        $staffUser = $userModel->withDeleted()
            ->where('login_mobile', $mobile)
            ->where('role_id !=', 3)
            ->first();

        return $staffUser ? self::STAFF_MOBILE_MESSAGE : null;
    }

    public static function nonPassengerRoleError(UserModel $userModel, int $userId): ?string
    {
        if ($userId <= 0) {
            return null;
        }

        $user = $userModel->withDeleted()->find($userId);
        if (! $user || (int) $user->role_id === 3) {
            return null;
        }

        return self::NON_PASSENGER_ROLE_MESSAGE;
    }

    /**
     * @param array<string, mixed> $profile
     */
    public static function ensurePassengerUserDetails(UserDetailModel $userDetailModel, int $userId, array $profile): void
    {
        if ($userDetailModel->where('user_id', $userId)->first()) {
            return;
        }

        $firstName = trim((string) ($profile['first_name'] ?? ''));
        if ($firstName === '') {
            return;
        }

        $userDetailModel->insert([
            'user_id' => $userId,
            'first_name' => $firstName,
            'last_name' => $profile['last_name'] ?? null,
            'id_type' => $profile['id_type'] ?? null,
            'country_id' => $profile['country_id'] ?? null,
            'id_number' => $profile['id_number'] ?? null,
            'address' => $profile['address'] ?? null,
            'city' => $profile['city'] ?? null,
            'zip_code' => $profile['zip_code'] ?? null,
        ]);
    }

    /**
     * @return array{first_name: string, last_name: string}
     */
    public static function passengerNameForUser(
        UserModel $userModel,
        UserDetailModel $userDetailModel,
        int $passengerId,
        ?string $bookingId = null
    ): array {
        $details = $userDetailModel->withDeleted()->where('user_id', $passengerId)->first();
        if ($details) {
            return [
                'first_name' => (string) ($details->first_name ?? ''),
                'last_name' => (string) ($details->last_name ?? ''),
            ];
        }

        $agent = model(AgentModel::class)->where('user_id', $passengerId)->first();
        if ($agent) {
            return [
                'first_name' => (string) ($agent->first_name ?? ''),
                'last_name' => (string) ($agent->last_name ?? ''),
            ];
        }

        if ($bookingId !== null && trim($bookingId) !== '') {
            $journey = model(JourneylistModel::class)
                ->where('booking_id', $bookingId)
                ->orderBy('id', 'ASC')
                ->first();
            if ($journey) {
                return [
                    'first_name' => (string) ($journey->first_name ?? ''),
                    'last_name' => (string) ($journey->last_name ?? ''),
                ];
            }
        }

        return ['first_name' => '', 'last_name' => ''];
    }
}
