<?php

namespace App\Components;


use App\Models\KeyDate;
use App\Services\HistoryService;
use Livewire\Component;

class KeyDateManager extends Component {

    public $keyDates = [];
    public ?string $name = '';
    public ?string $description = '';

    public bool $show = false;
    public bool $editing = false;
    public $keyDate;

    protected array $rules = [
        'name' => 'required|string|max:255',
    ];

    public function mount() {
        $this->authorize('key-dates.view');
        $this->loadData();
    }

    public function loadData() {
        $this->keyDates = KeyDate::orderBy('ordering')->get()->toArray();
    }

    public function showAdd() {
        $this->resetErrorBag();
        $this->editing = false;
        $this->show = true;
    }

    public function loadEdit(int $id) {
        $this->resetErrorBag();
        $date = KeyDate::find($id);

        $this->keyDate = $date;
        $this->name = $date->display_name;
        $this->description = $date->description;
        $this->editing = true;

        $this->show = true;
    }

    public function save(HistoryService $historyService) {
        $this->authorize($this->editing ? 'key-dates.edit' : 'key-dates.create');
        $data = $this->validate();

        if ($this->editing) {
            $this->keyDate->update([
                'name' => $this->name,
            ]);
            $historyService->add(auth()->user(), "Updated Key Date '{$this->name}'");
        } else {
            KeyDate::create([
                'display_name' => $this->name,
                'ordering' => KeyDate::count(),
            ]);
            $historyService->add(auth()->user(), "Created Key Date '{$this->name}'");
        }

        $this->loadData();
        $this->hideAdd();
    }

    public function hideAdd() {
        $this->reset('name', 'editing', 'keyDate');
        $this->show = false;
    }

    public function updateOrder(HistoryService $historyService, $orderedIds) {
        $this->authorize('key-dates.edit');
        foreach ($orderedIds as $index => $id) {
            KeyDate::where('id', $id)->update([
                'ordering' => $index,
            ]);
        }

        $this->loadData();
        $historyService->add(auth()->user(), "Modified Key Date order.");

        $this->dispatch('toast', message: "Successfully updated Key Date order.", type: 'success');
    }

    public function deleteKeyDate(HistoryService $service, int $id) {
        $this->authorize('key-dates.delete');
        $date = KeyDate::findOrFail($id);
        $name = $date->name;
        $date->delete();

        $service->add(auth()->user(), "Deleted Key Date '{$name}'");
        $this->loadData();
    }

    public function placeholder() {
        return <<<'HTML'
        <div class="row">
            <div class="col-12 col-md-6 offset-md-3">
                <div class="card">
                    <div class="card-body">
                        <div class="placeholder-glow">
                            <span class="placeholder col-12 mb-2"></span>
                            <span class="placeholder col-12 mb-2"></span>
                            <span class="placeholder col-8"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        HTML;
    }


    public function render() {
        return view('components.key-date-manager');
    }
}
