<?php

namespace Daylight\Core\Livewire\Tables\Concerns;

trait WithRowActions
{
    public ?int $confirmingDeleteId = null;

    public function confirmDelete(int $id): void
    {
        $this->confirmingDeleteId = $id;
    }

    public function cancelDelete(): void
    {
        $this->confirmingDeleteId = null;
    }

    public function deleteRow(int $id): void
    {
        $model = $this->getBaseQuery()->getModel()::findOrFail($id);

        $model->delete();

        $this->confirmingDeleteId = null;

        session()->flash('success', __('daylight::general.deleted'));
    }

    public function duplicateRow(int $id): void
    {
        $model = $this->getBaseQuery()->getModel()::findOrFail($id);

        $newModel = $this->performDuplicate($model);

        session()->flash('success', __('daylight::general.duplicated'));

        $this->redirect($this->getEditRoute($newModel));
    }

    protected function performDuplicate($model)
    {
        throw new \RuntimeException('performDuplicate() must be implemented in ' . static::class);
    }

    protected function getEditRoute($model): string
    {
        throw new \RuntimeException('getEditRoute() must be implemented in ' . static::class);
    }
}
