<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Maestro\Core\Modules\Offers\Enums\OfferSectionRole;

class OfferSection extends Model
{
    protected $table = 'offer_sections';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'offer_id',
        'parent_section_id',
        'title',
        'description_html',
        'sort_order',
        'section_role',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'section_role' => OfferSectionRole::class,
        ];
    }

    /**
     * @return BelongsTo<Offer, OfferSection>
     */
    public function offer(): BelongsTo
    {
        return $this->belongsTo(Offer::class);
    }

    /**
     * @return BelongsTo<OfferSection, OfferSection>
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(OfferSection::class, 'parent_section_id');
    }

    /**
     * Nested sections (pricing categories under {@see OfferSectionRole::PricingRoot}).
     *
     * @return HasMany<OfferSection, OfferSection>
     */
    public function children(): HasMany
    {
        return $this->hasMany(OfferSection::class, 'parent_section_id')->orderBy('sort_order');
    }

    /**
     * @return HasMany<OfferRow, OfferSection>
     */
    public function rows(): HasMany
    {
        return $this->hasMany(OfferRow::class)->orderBy('sort_order');
    }
}
