<?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;
use Laravel\Cashier\Subscription;

class ServiceBuildMilestone extends Model
{
    use HasFactory;

    public const STAGE_ORDERED = 'ordered';

    public const STAGE_HARDWARE_ACQUIRED = 'hardware_acquired';

    public const STAGE_ASSEMBLY = 'assembly';

    public const STAGE_RACKED = 'racked';

    public const STAGE_DEPLOYED = 'deployed';

    /**
     * Stage order is significant — used to drive the timeline UI and to
     * determine the "current" stage as the highest one with reached_at set.
     *
     * @var array<int, string>
     */
    public const STAGES = [
        self::STAGE_ORDERED,
        self::STAGE_HARDWARE_ACQUIRED,
        self::STAGE_ASSEMBLY,
        self::STAGE_RACKED,
        self::STAGE_DEPLOYED,
    ];

    protected $fillable = [
        'subscription_id',
        'stage',
        'reached_at',
        'note',
        'updated_by',
    ];

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

    public function subscription(): BelongsTo
    {
        return $this->belongsTo(Subscription::class);
    }

    public function updatedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }
}
