<?php

namespace App\Models;

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

class TrustEvent extends Model
{
    use HasFactory;

    protected $fillable = [
        'media_owner_id',
        'placement_id',
        'booking_item_id',
        'event_type',
        'score_before',
        'score_after',
        'score_delta',
        'notes',
        'triggered_by',
    ];

    protected $casts = [
        'score_before' => 'decimal:2',
        'score_after'  => 'decimal:2',
        'score_delta'  => 'decimal:2',
    ];

    // ============================================================
    // Scopes
    // ============================================================
    public function scopeForOwner($query, int $mediaOwnerId)
    {
        return $query->where('media_owner_id', $mediaOwnerId);
    }

    public function scopePositive($query)
    {
        return $query->where('score_delta', '>', 0);
    }

    public function scopeNegative($query)
    {
        return $query->where('score_delta', '<', 0);
    }

    // ============================================================
    // Helpers
    // ============================================================
    public function isPositive(): bool
    {
        return $this->score_delta > 0;
    }

    public function isNegative(): bool
    {
        return $this->score_delta < 0;
    }

    public function getEventLabel(): string
    {
        return match($this->event_type) {
            'proof_on_time'         => 'Proof On Time',
            'proof_late'            => 'Proof Late',
            'proof_missed'          => 'Proof Missed',
            'proof_passed'          => 'Proof Passed',
            'proof_failed'          => 'Proof Failed',
            'dispute_raised'        => 'Dispute Raised',
            'dispute_won'           => 'Dispute Won',
            'dispute_lost'          => 'Dispute Lost',
            'cancellation'          => 'Cancellation',
            'instant_book_granted'  => 'Instant Book Granted',
            'instant_book_revoked'  => 'Instant Book Revoked',
            'manual_adjustment'     => 'Manual Adjustment',
            default                 => ucfirst($this->event_type),
        };
    }

    public function getDeltaBadgeClass(): string
    {
        if ($this->score_delta > 0) return 'nx-badge-good';
        if ($this->score_delta < 0) return 'nx-badge-danger';
        return 'nx-badge';
    }

    public function getFormattedDelta(): string
    {
        $prefix = $this->score_delta >= 0 ? '+' : '';
        return $prefix . number_format($this->score_delta, 2);
    }

    // ============================================================
    // Relationships
    // ============================================================
    public function mediaOwner()
    {
        return $this->belongsTo(MediaOwner::class);
    }

    public function placement()
    {
        return $this->belongsTo(Placement::class);
    }

    public function bookingItem()
    {
        return $this->belongsTo(BookingItem::class);
    }

    public function triggeredBy()
    {
        return $this->belongsTo(User::class, 'triggered_by');
    }
}