<?php
namespace Model;
use Entity\SourceEntity;
use Enum\TypeSourceEnum;

class SourceModel extends Model
{

    public function createSource(string $title, string $date, TypeSourceEnum $type) : bool
    {
        $id = $this -> gateway -> getLastId();
        $q = new SourceEntity($id,$title, $date, $type);
        return $this -> gateway -> create($q);
        
    }

    public function getSourceById(int $id_source) : ?SourceEntity
    {
        $res = $this -> gateway -> findById($id_source);
        
        if ($res)
            return new sourceEntity(
                $res[0]["id_source"],
                $res[0]["title"],
                $res[0]["dates"],
                TypeSourceEnum::Movie//from($res[0]["type"])
            );
        return null;
    }

    public function getSourceByTitle(string $title) : ?SourceEntity
    {
        $res = $this->gateway->findByTitle($title);
        if ($res)
            return new sourceEntity(
                $res[0]["id_source"],
                $res[0]["title"],
                $res[0]["dates"],
                TypeSourceEnum::Movie//from($res[0]["type"])
            );
        return null;
    }

    public function getSourceByDate(string $date) : array
    {
        $res = $this->gateway->findByDate($date);
        $src = [];
        foreach ($res as $sources) {
            $src[] = new sourceEntity(
                $sources["id_source"],
                $sources["title"],
                $sources["dates"],
                TypeSourceEnum::from($sources["type"])
            );
        }
        return $src;
    }

    public function getSourceByType(TypeSourceEnum $type) : array
    {
        $res = $this->gateway->findByType($type);
        $src = [];
        foreach ($res as $sources) {
            $src[] = new sourceEntity(
                $sources["id_source"],
                $sources["title"],
                $sources["dates"],
                TypeSourceEnum::from($sources["type"])
            );
        }
        return $src;
    }

    public function getAllSources() : array
    {
        $res =  $this -> gateway -> findAll();
        $src = [];
        foreach ($res as $sources) {
            $src[] = new SourceEntity(
                $sources["id_source"],
                $sources["title"],
                $sources["dates"],
                TypeSourceEnum::Movie
                //TypeSourceEnum::from($sources["type"])
            );
        }
        return $src;
    }

    public function deleteSource(int $id_source) : bool
    {
        return $this -> gateway -> delete($id_source);
    }

    public function updateSource(int $id_source, string $title, string $date) : bool
    {
        $q = $this -> getSourceById($id_source);

        if ($q){
            $q -> setTitle($title);
            $q -> setDate($date);
            return $this -> gateway -> update($q);
        }
        return false;
    }

    public function existSource(string $name, string $type) : bool{
        $q = $this -> getSourceByTitle($name);
        return isset($q);
    }
}

