<?php

namespace Build\Entities;

class ProjectsArgs
{
    public function __construct(
        public bool|null $isDeployedLocally = null,
        public string|null $name = null,
        public bool|null $isLocal = null,
        public int|null $limit = 10,
        public int|null $skip = 0,
    ) {}

    public function toWhere(): array
    {
        $data = [];

        if ($this->isDeployedLocally !== null) {
            $data['is_deployed_locally'] = $this->isDeployedLocally ? 1 : 0;
        }

        if ($this->name !== null) {
            $data['name'] = $this->name;
        }

        if ($this->isLocal !== null) {
            $data['is_local'] = $this->isLocal ? 1 : 0;
        }

        return $data;
    }

    /**
     * @param array $data
     * @return ProjectsArgs
     */
    public static function fromArray(array $data): self
    {
        return new self(
            isDeployedLocally: isset($data['is_deployed_locally']) ? $data['is_deployed_locally'] === 'true' : null,
            name: $data['name'] ?? null,
            isLocal: isset($data['is_local']) ? $data['is_local'] === 'true' : null,
            limit: isset($data['limit']) ? (int)$data['limit'] : 10,
            skip: isset($data['skip']) ? (int)$data['skip'] : 0
        );
    }
}
