<?php

declare(strict_types=1);

namespace App\Models;

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

class PlanConfigValue extends Model
{
    use HasFactory;

    protected $fillable = [
        'option_id',
        'label',
        'value',
        'hourly_price',
        'monthly_price',
        'quarterly_price',
        'semi_annual_price',
        'annual_price',
        'is_default',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'hourly_price' => 'decimal:4',
            'monthly_price' => 'decimal:2',
            'quarterly_price' => 'decimal:2',
            'semi_annual_price' => 'decimal:2',
            'annual_price' => 'decimal:2',
            'is_default' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

    public function option(): BelongsTo
    {
        return $this->belongsTo(PlanConfigOption::class, 'option_id');
    }

    public function getPriceForCycle(string $cycle): float
    {
        $priceField = match ($cycle) {
            'hourly' => 'hourly_price',
            'monthly' => 'monthly_price',
            'quarterly' => 'quarterly_price',
            'semi_annual' => 'semi_annual_price',
            'annual' => 'annual_price',
            default => 'monthly_price',
        };

        return (float) ($this->{$priceField} ?? 0);
    }
}
