<?php

namespace App\Models;

use App\enum\ProviderType;
use App\enum\UsernameType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Username extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $fillable = [
        'username',
        'is_active',
        'daily_mailbox_limit',
        'username_type',
        'provider_type',
        'starts_at',
        'ends_at',
        'last_used_at',
        'checked_at',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'daily_mailbox_limit' => 'integer',
            'starts_at' => 'datetime',
            'ends_at' => 'datetime',
            'last_used_at' => 'datetime',
            'checked_at' => 'datetime',
        ];
    }

    /**
     * Retrieve active username by type and provider.
     *
     * @param  UsernameType|null  $usernameType  Filter by username type
     * @param  ProviderType|null  $providerType  Filter by provider type
     * @return array Array of usernames
     */
    public static function getActiveUsernameByType(
        ?UsernameType $usernameType = null,
        ?ProviderType $providerType = null,
    ): array {
        $query = static::query()
            ->where('is_active', true)
            ->where(function ($query) {
                $query->whereNull('starts_at')
                    ->orWhere('starts_at', '<=', now());
            })
            ->where(function ($query) {
                $query->whereNull('ends_at')
                    ->orWhere('ends_at', '>=', now());
            });

        if ($usernameType) {
            $query->where('username_type', $usernameType);
        }

        if ($providerType) {
            $query->where('provider_type', $providerType);
        }

        return $query->pluck('username')->toArray();
    }

    /**
     * Retrieve all active usernames grouped by type and provider.
     *
     * @return array Array with gmailUsernames, premium_gmailUsernames, outlookUsernames, premium_outlookUsernames keys
     */
    public static function getActiveUsernames(): array
    {
        $usernames = static::query()
            ->where('is_active', true)
            ->where(function ($query) {
                $query->whereNull('starts_at')
                    ->orWhere('starts_at', '<=', now());
            })
            ->where(function ($query) {
                $query->whereNull('ends_at')
                    ->orWhere('ends_at', '>=', now());
            })
            ->select('username', 'username_type', 'provider_type')
            ->get()
            ->groupBy(['provider_type', 'username_type']);

        $gmailPublic = $usernames->get(ProviderType::GMAIL->value, collect())->get(UsernameType::PUBLIC->value, collect())->pluck('username')->toArray();
        $gmailPremium = $usernames->get(ProviderType::GMAIL->value, collect())->get(UsernameType::PREMIUM->value, collect())->pluck('username')->toArray();
        $outlookPublic = $usernames->get(ProviderType::OUTLOOK->value, collect())->get(UsernameType::PUBLIC->value, collect())->pluck('username')->toArray();
        $outlookPremium = $usernames->get(ProviderType::OUTLOOK->value, collect())->get(UsernameType::PREMIUM->value, collect())->pluck('username')->toArray();

        return [
            'gmailUsernames' => $gmailPublic,
            'premium_gmailUsernames' => $gmailPremium,
            'outlookUsernames' => $outlookPublic,
            'premium_outlookUsernames' => $outlookPremium,
        ];
    }
}
