<?php

namespace App\Nexus\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class NexusSession extends Model
{
    protected $table = 'nexus_sessions';

    protected $fillable = [
        'session_number',
        'session_date',
        'scheduled_start',
        'actual_start',
        'actual_end',
        'status',
        'total_bid_cost',
        'total_sell_value',
        'win_rate_target',
        'win_rate_actual',
        'total_bids',
        'total_sells_evaluated',
        'total_sells_approved',
        'total_sells_rejected',
        'criteria_order',
        'session_notes',
    ];

    protected $casts = [
        'session_date'          => 'date',
        'scheduled_start'       => 'datetime',
        'actual_start'          => 'datetime',
        'actual_end'            => 'datetime',
        'total_bid_cost'        => 'decimal:2',
        'total_sell_value'      => 'decimal:2',
        'win_rate_target'       => 'decimal:2',
        'win_rate_actual'       => 'decimal:2',
        'criteria_order'        => 'array',
    ];

    // ─── STATUS CONSTANTS ───
    const STATUS_PENDING    = 'pending';
    const STATUS_ACTIVE     = 'active';
    const STATUS_PROCESSING = 'processing';
    const STATUS_COMPLETED  = 'completed';
    const STATUS_CANCELLED  = 'cancelled';

    // ─── RELATIONSHIPS ───

    public function sells()
    {
        return $this->hasMany(\App\Models\Sell::class, 'nexus_session_id');
    }

    public function bids()
    {
        return $this->hasMany(\App\Models\Bid::class, 'nexus_session_id');
    }

    public function auditLogs()
    {
        return $this->hasMany(NexusAuditLog::class, 'nexus_session_id');
    }

    public function economySnapshots()
    {
        return $this->hasMany(NexusEconomySnapshot::class, 'nexus_session_id');
    }

    // ─── SCOPES ───

    public function scopeToday(Builder $query): Builder
    {
        return $query->whereDate('session_date', today());
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('status', self::STATUS_ACTIVE);
    }

    public function scopePending(Builder $query): Builder
    {
        return $query->where('status', self::STATUS_PENDING);
    }

    public function scopeCompleted(Builder $query): Builder
    {
        return $query->where('status', self::STATUS_COMPLETED);
    }

    // ─── HELPERS ───

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

    public function isCompleted(): bool
    {
        return $this->status === self::STATUS_COMPLETED;
    }

    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    /**
     * Win rate ratio — total_sell_value / total_bid_cost
     * Used to determine which win rate tier applies (80/50/30%)
     */
    public function sellToBidRatio(): float
    {
        if ($this->total_bid_cost <= 0) return 0;
        return (float) ($this->total_sell_value / $this->total_bid_cost);
    }

    /**
     * Which win rate target applies based on ratio
     * Ratio <= 0.5  → 80%
     * Ratio 0.5–1.0 → 50%
     * Ratio > 1.0   → 30%
     */
    public function resolveWinRateTarget(): float
    {
        $ratio = $this->sellToBidRatio();
        if ($ratio <= 0.5)  return 80.0;
        if ($ratio <= 1.0)  return 50.0;
        return 30.0;
    }
}
