from Outils.Directory import GenerateDirectory
from pathlib import Path

def createFileRepository(nom, specifique = ''):

    generateDirectory = GenerateDirectory(nom, "/Modules/"+nom+"/Repository", specifique)
    directory = generateDirectory.getDirectory()
    namespace = generateDirectory.getNamespace()
    nom = generateDirectory.getNom()

    Path(directory).mkdir(parents=True, exist_ok=True)

    with open(directory+"/Gestion"+nom+"Repository.php","x", encoding='utf-8') as file:
        file.write("<?php\n\n")
        file.write("namespace "+namespace+";\n\n")
        file.write("class Gestion"+nom+"Repository\n")
        file.write("{\n\n")
        file.write("    private $pdo;\n\n")
        file.write("    public function __construct(\PDO $pdo)\n")
        file.write("    {\n")
        file.write("        $this->pdo = $pdo;\n")
        file.write("    }\n\n")
        file.write("    public function gestion"+nom+"()\n")
        file.write("    {\n")
        file.write("        $query = 'SELECT CODE\n")
        file.write("            FROM FOURNISS \n")
        file.write("            WHERE CODE = :CODE';\n\n")
        file.write("        $data = $this->pdo->prepare($query);\n\n")
        file.write("        $data->bindParam(':CODE', 'MYCODE', \PDO::PARAM_STR);\n\n")
        file.write("        $data->execute();\n\n")
        file.write("        return $data->fetch(\PDO::FETCH_ASSOC);\n\n")
        file.write("    }\n")
        file.write("}\n")
        file.close()

