<?php

namespace App\Services\Bot;

use Carbon\Carbon;

class RegimeDetector
{
    public function detect(float $spot, float $vwap, float $ema20, float $ema50, ?Carbon $expiryDate = null): string
    {
        if ($expiryDate && $expiryDate->isToday()) {
            return 'EXPIRY_DAY';
        }

        if ($spot > $vwap && $ema20 > $ema50) {
            return 'TRENDING_UP';
        }

        if ($spot < $vwap && $ema20 < $ema50) {
            return 'TRENDING_DOWN';
        }

        return 'SIDEWAYS';
    }

    public function preferredOption(string $regime, array $rulebook): ?string
    {
        return match ($regime) {
            'TRENDING_UP' => $rulebook['market_regimes']['TRENDING_UP']['preferred_option'] ?? 'CE',
            'TRENDING_DOWN' => $rulebook['market_regimes']['TRENDING_DOWN']['preferred_option'] ?? 'PE',
            default => null,
        };
    }
}
