<?php

namespace App\Modules\Template\TourTemplate\Domain;

use App\Modules\Shared\Domain\Storage\Criteria\StorageCriteria;
use App\Modules\Tour\Domain\IdTour;
use Illuminate\Support\Facades\DB;

class ListTourTemplate implements StorageCriteria
{

    public function __construct(
        private string $action
    ) {}

    public function run()
    {
        return $this->analize($this->query());
    }

    protected function query()
    {
        $select = TourTemplate::select('`l`');
        $table = TourTemplate::table();
        if ($this->action == 'all') {
            $where = '`l`.`TourTemplate_Status` IN (1, 2)';
        } else if ($this->action == 'active') {
            $where = '`l`.`TourTemplate_Status` = 2';
        } else if ($this->action == 'deleted') {
            $where = '`l`.`TourTemplate_Status` = 0';
        } else {
            $where = '`l`.`TourTemplate_Status` = 2';
        }
        return DB::select(
            "SELECT $select
            FROM `$table` `l`
            WHERE $where
            ORDER BY `TourTemplate_Name` ASC",
            []
        );
    }

    protected function analize($data)
    {
        return array_map(fn($dto) => TourTemplate::fromDTO($dto), $data);
    }
}
