<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class EventClaim extends Model
{
    protected $table = 'event_claims';

    protected $fillable = [
        'event_id',
        'user_id',
        'role_id',
        'role_name',
        'claimed_at',
        'mail_sent',
        'ip_address',
    ];

    protected $casts = [
        'event_id' => 'integer',
        'user_id' => 'integer',
        'role_id' => 'integer',
        'claimed_at' => 'datetime',
        'mail_sent' => 'boolean',
    ];

    /**
     * Check if an IP has already claimed a specific event
     */
    public static function hasIpClaimedEvent(string $ipAddress, int $eventId): bool
    {
        return self::where('ip_address', $ipAddress)
            ->where('event_id', $eventId)
            ->whereNotNull('claimed_at')
            ->exists();
    }

    /**
     * Get the count of claims from a specific IP for an event
     */
    public static function getIpClaimCountForEvent(string $ipAddress, int $eventId): int
    {
        return self::where('ip_address', $ipAddress)
            ->where('event_id', $eventId)
            ->whereNotNull('claimed_at')
            ->count();
    }

    /**
     * Get all claims from an IP address
     */
    public static function getClaimsByIp(string $ipAddress)
    {
        return self::where('ip_address', $ipAddress)
            ->whereNotNull('claimed_at')
            ->with('event')
            ->get();
    }

    /**
     * Get the event associated with this claim
     */
    public function event(): BelongsTo
    {
        return $this->belongsTo(Event::class, 'event_id');
    }

    /**
     * Get the user who claimed
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'ID');
    }

    /**
     * Check if user has already claimed for an event
     */
    public static function hasUserClaimed(int $userId, int $eventId): bool
    {
        return self::where('user_id', $userId)
            ->where('event_id', $eventId)
            ->whereNotNull('claimed_at')
            ->exists();
    }

    /**
     * Get claim for user in an event
     */
    public static function getUserClaim(int $userId, int $eventId)
    {
        return self::where('user_id', $userId)
            ->where('event_id', $eventId)
            ->first();
    }

    /**
     * Get or create eligible claim record for user
     */
    public static function getOrCreateEligibleClaim(int $userId, int $eventId)
    {
        return self::firstOrCreate(
            [
                'user_id' => $userId,
                'event_id' => $eventId,
            ],
            [
                'claimed_at' => null,
                'mail_sent' => false,
            ]
        );
    }

    /**
     * Get all claims for an event
     */
    public static function getClaimsForEvent(int $eventId)
    {
        return self::with(['user', 'event'])
            ->where('event_id', $eventId)
            ->orderBy('claimed_at', 'desc')
            ->get();
    }
}
