<?php

namespace Licenzo\Model;

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

use Licenzo\Framework\Database\Model;

class WebhookLog extends Model
{
    protected $table = 'licenzo_webhook_logs';

    // Provider constants
    const PROVIDER_STRIPE = 'stripe';
    const PROVIDER_PAYPAL = 'paypal';
    const PROVIDER_WOOCOMMERCE = 'woocommerce';

    // Status constants
    const STATUS_PENDING = 'pending';
    const STATUS_PROCESSED = 'processed';
    const STATUS_FAILED = 'failed';

    // Retry limit
    const MAX_RETRY_COUNT = 5;

    protected $fillable = [
        'provider', 'event_type', 'event_id',
        'payload', 'headers',
        'status', 'error_message', 'retry_count',
        'processed_at'
    ];

    protected $casts = [
        'id' => 'int',
        'retry_count' => 'int',
        'payload' => 'array',
        'headers' => 'array',
        'processed_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    // Scopes
    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeProcessed($query)
    {
        return $query->where('status', 'processed');
    }

    public function scopeFailed($query)
    {
        return $query->where('status', 'failed');
    }

    public function scopeByProvider($query, $provider)
    {
        return $query->where('provider', $provider);
    }

    public function scopeByEventType($query, $eventType)
    {
        return $query->where('event_type', $eventType);
    }

    public function scopeNeedsRetry($query)
    {
        return $query->where('status', 'failed')
                     ->where('retry_count', '<', 5);
    }

    // Helpers
    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isProcessed(): bool
    {
        return $this->status === 'processed';
    }

    public function isFailed(): bool
    {
        return $this->status === 'failed';
    }

    public function canRetry(): bool
    {
        return $this->status === static::STATUS_FAILED && $this->retry_count < static::MAX_RETRY_COUNT;
    }

    public function markAsProcessed()
    {
        $this->update([
            'status' => 'processed',
            'processed_at' => gmdate('Y-m-d H:i:s')
        ]);
    }

    public function markAsFailed($errorMessage)
    {
        $this->update([
            'status' => 'failed',
            'error_message' => $errorMessage,
            'retry_count' => $this->retry_count + 1
        ]);
    }

    public function incrementRetry()
    {
        $this->increment('retry_count');
    }

    public function getPayloadData($key = null, $default = null)
    {
        if ($key === null) {
            return $this->payload;
        }
        return $this->payload[$key] ?? $default;
    }

    public function getHeaderData($key = null, $default = null)
    {
        if ($key === null) {
            return $this->headers;
        }
        return $this->headers[$key] ?? $default;
    }
}

