<?php

namespace App\Http\Livewire;

use LaravelViews\Views\TableView;
use App\Models\Especiality;
use App\Actions\DeleteEspeciality;
use App\Actions\CreateHistoryFromEspecialityAction;
use App\Actions\ActiveEspeciality;
use App\Http\Livewire\ConfigSearch;

class AreasTableView extends TableView
{
    use ConfigSearch;
    /**
     * Sets a model class to get the initial data
     */
    protected $model = Especiality::class;
    
    public $searchBy = ['id', 'name'];

    /**
     * Define el repositorio para obtener todas las especialidades sin el scope global
     */
    public function repository()
    {
        return Especiality::query()->withoutGlobalScope('active');
    }

    /**
     * Sets the headers of the table as you want to be displayed.
     *
     * @return array<string> Array of headers.
     */
    public function headers(): array
    {
        return [
            __('common-table.table.name'),
            __('common-table.table.appointments'),
            __('common-table.table.histories'),
            __('common-table.table.status')
        ];
    }

    /**
     * Sets the data for each cell of a single row.
     *
     * @param $model Current model for each row.
     */
    public function row($model): array
    {
        return [
            strtoupper($model->name),
            count($model->appointments()->get()),
            count($model->histories()->get()),
            $this->getStatus($model)
        ];
    }

    public function getStatus($model)
    {
        return $model->active ? __('common-table.table.active') : __('common-table.table.inactive');
    }   

      /** For actions by item */
      protected function actionsByRow(){
        return [
           new CreateHistoryFromEspecialityAction,
           new DeleteEspeciality,
           new ActiveEspeciality
        ];
    }
}
