<?php

namespace Src\Modules\JsonObjects\Infrastructure;

use Src\Modules\JsonObjects\Interfaces\Infrastructure\IDirsQuery;
use Src\Common\Infrastructure\TraitSqlQuery;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query\Builder;

class DirsQuery implements IDirsQuery
{

    use TraitSqlQuery {
        TraitSqlQuery::getSelectSection as baseGetSelectSection;
    }

    protected string $tableName;

    protected Builder $queryBuilder;

    public function setTableName(string $tableName)
    {
        $this->tableName = $tableName;
    }

    protected function reset()
    {
        $this->queryBuilder = DB::table($this->tableName);
    }

    // protected function getSelectSection(array $fields, array $allowFields, string $table, string $prefix = ''): array
    // {
    //     $segments = $this->baseGetSelectSection($fields, $allowFields, $table, $prefix);
    //     $result = [];
    //     foreach ($segments as $field => $alias) {
    //         $result[] = $field . ' AS ' . $alias;
    //     }
    //     return $result;
    // }

    public function select(array $fields)
    {
        $this->reset();
        $selectSection = $this->_getSelectSection($fields, ['id', 'matherial_path', 'parent_id', 'name'], $this->tableName);
        $this->queryBuilder->select($selectSection);
        return $this;
    }

    public function whereId($id)
    {
        $this->queryBuilder->where($this->tableName . '.id', '=', $id);
        return $this;
    }

    public function whereIdIn(array $ids)
    {
        $this->queryBuilder->whereIn($this->tableName . '.id', $ids);
        return $this;
    }

    public function whereParentId($parentId)
    {
        $this->queryBuilder->where($this->tableName . '.parent_id', '=', $parentId);
        return $this;
    }

    public function whereInPath(string $matherialPath)
    {
        $this->queryBuilder->where($this->tableName . '.matherial_path', 'like', $matherialPath . '%');
        return $this;
    }

    // public function withParent(array $fields)
    // {
    //     $this->joinParend();
    //     $selectSection = $this->getSelectSection($fields, ['id', 'matherial_path', 'parent_id', 'name'], $this->tableName, 'parent_');
    //     $this->queryBuilder->addSelect($selectSection);
    //     return $this;
    // }

    // protected function joinParend()
    // {
    //     if (!$this->isParentJoined) {
    //         $first = $this->tableName . '.id';
    //         $two = $this->tableName . '.parent_id';
    //         $this->queryBuilder->leftJoin($this->tableName . ' AS parent', $first, '=', $two);
    //         $this->isParentJoined = true;
    //     }
    // }

    public function all(): array
    {
        $result = [];
        foreach ($this->queryBuilder->get()->all() as $row) {
            $result[$row->id] = $row;
        }
        return json_decode(json_encode($result), true);
    }

    public function one(): array
    {
        $result = $this->queryBuilder->first() ?? [];
        $result = json_decode(json_encode($result), true);
        return $result;
    }
}
