<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Instrument extends Model
{
    protected $fillable = [
        'exchange',
        'token',
        'tradingsymbol',
        'underlying',
        'expiry_date',
        'strike_price',
        'option_type',
        'lot_size',
        'tick_size',
        'is_active',
        'instrument_kind',
    ];

    protected function casts(): array
    {
        return [
            'expiry_date' => 'date',
            'strike_price' => 'decimal:2',
            'tick_size' => 'decimal:4',
            'is_active' => 'boolean',
        ];
    }

    public function marketTicks(): HasMany
    {
        return $this->hasMany(MarketTick::class);
    }

    public function optionGreeks(): HasMany
    {
        return $this->hasMany(OptionGreek::class);
    }

    public function latestGreek()
    {
        return $this->hasOne(OptionGreek::class)->latestOfMany('computed_at');
    }

    public function latestTick()
    {
        return $this->hasOne(MarketTick::class)->latestOfMany('tick_at');
    }
}
