<?php

namespace Licenzo\Model;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Licenzo\Framework\Database\Model;

/**
 * LicenseActivation Model
 *
 * Tracks activations of licenses on instances (domains, devices, etc.)
 */
class LicenseActivation extends Model
{
    protected $table = 'licenzo_license_activations';

    // Status constants
    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';
    const STATUS_DEACTIVATED = 'deactivated';
    const STATUS_REVOKED = 'revoked';
    const STATUS_EXPIRED = 'expired';

    // Activation method constants
    const METHOD_KEY_BASED = 'key_based';
    const METHOD_HASH_BASED = 'hash_based';
    const METHOD_TOKEN_BASED = 'token_based';

    protected $fillable = [
        'license_id',
        'product_id',
        'variation_id',
        'product_name',
        'variation_name',
        'instance_identifier',
        'activation_hash',
        'status',
        'ip_address',
        'user_agent',
        'activated_at',
        'deactivated_at',
        'last_validated_at',
        'activation_method',
        'expire_date',
        'meta',
    ];

    protected $casts = [
        'id'                => 'int',
        'license_id'        => 'int',
        'product_id'        => 'int',
        'variation_id'      => 'int',
        'activated_at'      => 'datetime',
        'deactivated_at'    => 'datetime',
        'last_validated_at' => 'datetime',
        'expire_date'       => 'datetime',
        'meta'              => 'array',
        'created_at'        => 'datetime',
        'updated_at'        => 'datetime'
    ];

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

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function variation()
    {
        return $this->belongsTo(ProductVariation::class, 'variation_id');
    }

    public function events()
    {
        return $this->hasMany(LicenseEvent::class, 'activation_id');
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('status', static::STATUS_ACTIVE);
    }

    public function scopeInactive($query)
    {
        return $query->where('status', static::STATUS_INACTIVE);
    }

    public function scopeRevoked($query)
    {
        return $query->where('status', static::STATUS_REVOKED);
    }

    public function scopeByInstance($query, $identifier)
    {
        return $query->where('instance_identifier', $identifier);
    }

    public function scopeByHash($query, $hash)
    {
        return $query->where('activation_hash', $hash);
    }

    // Helpers
    public function isActive(): bool
    {
        return $this->status === static::STATUS_ACTIVE;
    }

    public function isInactive(): bool
    {
        return $this->status === static::STATUS_INACTIVE;
    }

    public function isRevoked(): bool
    {
        return $this->status === static::STATUS_REVOKED;
    }

    public function isExpired(): bool
    {
        return $this->status === static::STATUS_EXPIRED;
    }

    public function deactivate(): bool
    {
        $this->status = static::STATUS_DEACTIVATED;
        $this->deactivated_at = gmdate('Y-m-d H:i:s');
        return $this->save();
    }

    public function validate(): bool
    {
        $this->last_validated_at = gmdate('Y-m-d H:i:s');
        return $this->save();
    }
}

