<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Pessimistic single-editor lock for a project (Team Workspaces P4 / Δ21).
 *
 * One row per project. The holder heartbeats every `HEARTBEAT_INTERVAL`
 * while the editor tab is visible; a lock whose `last_heartbeat_at` is older
 * than `ProjectEditLockController::LOCK_TTL` is considered free on the next
 * acquire (expiry-on-read).
 */
#[Fillable([
    'project_id',
    'user_id',
    'acquired_at',
    'last_heartbeat_at',
])]
class ProjectEditLock extends Model
{
    protected function casts(): array
    {
        return [
            'acquired_at' => 'datetime',
            'last_heartbeat_at' => 'datetime',
        ];
    }

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

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