<?php

namespace App\Livewire\Phases;

use App\Models\Phase;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.app')]
class PhaseProgress extends Component
{
    public Phase $phase;

    public $progress;

    public $comment = '';

    public function mount(Phase $phase)
    {
        $this->phase = $phase->load('progressUpdates');
        $this->progress = $phase->progress_percent;
    }

    public function updateProgressManual()
    {
        $this->validate(['progress' => 'required|integer|min:0|max:100']);
        $this->phase->progress_percent = $this->progress;
        $this->phase->save();

        $this->phase->progressUpdates()->create([
            'user_id' => auth()->id(),
            'progress_percent' => $this->progress,
            'comment' => $this->comment,
        ]);

        $this->dispatch('progressUpdated', $this->phase->id, $this->progress);
        session()->flash('message', 'Progreso actualizado');
    }

    public function render()
    {
        return view('livewire.phases.phase-progress');
    }
}
