<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LicenseActivation extends Model
{
    use HasFactory;

    protected $fillable = [
        'license_id',
        'domain',
        'token',
        'status',
        'activated_at',
        'last_seen_at',
        'plugin_version',
    ];

    protected $casts = [
        'activated_at' => 'datetime',
        'last_seen_at' => 'datetime',
    ];

    public function license()
    {
        return $this->belongsTo(License::class);
    }

    public function aiRequests()
    {
        return $this->hasMany(AiRequest::class);
    }

    public function todayRequestCount()
    {
        return $this->aiRequests()->whereDate('created_at', today())->count();
    }

    public function todayTokenCount()
    {
        return $this->aiRequests()->whereDate('created_at', today())->sum('tokens_used');
    }
}
