<?php

namespace App\Modules\PaxInformationRoom\Domain;

use App\Modules\PaxInformation\Domain\IdPaxInformation;
use App\Modules\PaxInformation\Domain\PaxInformation;
use App\Modules\PaxInformationRoom\Application\List\ListPaxInformationRoomPopulate as Populate;
use App\Modules\PaxInformationRoomPassenger\Domain\PaxInformationRoomPassenger;
use App\Modules\Shared\Domain\QueryBuilder\QueryBuilder;
use App\Modules\Shared\Domain\Storage\Criteria\StorageCriteria;
use App\Modules\TypeRoom\Domain\TypeRoom;
use Illuminate\Support\Facades\DB;

class ListPaxInformationRoom implements StorageCriteria
{
    private QueryBuilder $queryBuilder;

    private $actionList = [
        'all' => [1, 2],
        'active' => [2],
        'inactive' => [1],
        'deleted' => [0]
    ];

    public function __construct(
        private IdPaxInformation $idPaxInformation,
        private string $action,
        private Populate $populate,
    ) {
        $this->queryBuilder = new QueryBuilder();
    }

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

    protected function query()
    {
        $populateStrings = [
            Populate::PAX_INFORMATION => [PaxInformation::class, 'Id_PaxInformation', 'ls', 'INNER', 'setPaxInformation'],
            Populate::TYPE_ROOM => [TypeRoom::class, 'Id_TypeRoom', 'le', 'INNER', 'setTypeRoom'],
        ];

        $queryBuilder = $this->queryBuilder->select(PaxInformationRoom::class, 'r')
            ->where('`r`.`Id_PaxInformation`', '=', $this->idPaxInformation->value())
            ->where('`r`.`PaxInformationRoom_Status`', '<>', PaxInformationRoomStatus::DELETED);

        foreach ($this->populate->getValue() as $populate) {
            if (array_key_exists($populate, $populateStrings)) {
                $queryBuilder->populate(...$populateStrings[$populate]);
            }
        }

        list($query, $values) = $queryBuilder->build();
        return DB::select(
            $query,
            $values
        );

        // $where = new OperatorAnd(
        //     new OperatorEqual(
        //         '`pi`.`Id_PaxInformation`',
        //         $this->idPaxInformation->value(),
        //     ),
        //     new OperatorIn('`pi`.`PaxInformationRoom_Status`', $this->findActionElement($this->action))
        // );
        // return DB::select(
        //     "SELECT $select
        //     FROM `$table` `pi`

        //     WHERE {$where->query()}
        //     ORDER BY `pi`.`Id_PaxInformationRoom` ASC",
        //     $where->values()
        // );
    }

    private function findActionElement($action)
    {
        if (array_key_exists($action, $this->actionList)) {
            return $this->actionList[$action];
        }
        return $this->actionList['active'];
    }

    protected function analize($data)
    {
        $models = [];
        foreach ($data as $element) {
            $model = $this->queryBuilder->mainTable()->createFromDTO($element);
            foreach ($this->queryBuilder->populates() as $populate) {
                $model->{$populate['setter']}($populate['table']->createFromDTO($data[0]));
            }
            $models[] = $model;
        }
        return $models;
    }
}
