<?php

namespace Korkz\Demirteknoloji\Models\Admin;
use Exception;
use Korkz\Demirteknoloji\Connect\DbOperations;
use Korkz\Demirteknoloji\traits\VerotUpload;

class ReferencesModel extends DbOperations
{
    use VerotUpload;

    /**
     * @param $file
     * @param $option
     * @param $lang
     * @param $op
     * @return Exception|bool|array
     */
    protected function refFileUploadedModel($file, $option, $lang, $op): Exception|bool|array
    {
        $pdo = $this->getPdo();
        $pdo->beginTransaction();
        try {
            $uploaded = $this->fileUpload($file, $option, $lang);
            $reference = null;
            if ($uploaded["boolean"]) {
                switch ($op) {
                    case "insert":
                        $languages = ["tr", "en"];
                        $requestAdd = [];
                        foreach ($languages as $language) {
                            $reference = $this->insertData("dm_references", ["Lang" => $language, "file_path" => $uploaded["path"]], "ID", true);
                            if ($reference["boolean"]) {
                                $requestAdd[] = ["lang" => $language, "lastInsertId" => $reference["lastInsertId"]];
                            }
                        }
                        if ($reference) {
                            $pdo->commit();
                            return ["boolean" => true, "requestAdds" => $requestAdd];
                        } else {
                            $pdo->rollBack();
                            return false;
                        }
                    case "update";
//                        $references = $this->getData("dm_references");
//                        $reference = $this->updateData("dm_references", [ucwords($fileToWhere) => $uploaded["path"], "updated_at" => date("Y-m-d H:i:s")], ["Lang" => $file_language]);
                        if ($reference) {
                            $pdo->commit();
                            return true;
                        } else {
                            $pdo->rollBack();
                            return false;
                        }
                        break;
                    default:
                        $pdo->rollBack();
                        return false;

                }

            } else {
                $pdo->rollBack();
                return false;
            }
        } catch (Exception $exception) {
            $pdo->rollBack();
            http_response_code(500);
            return $exception;
        }
    }
    protected function refModel(array $requestAdds,string $op): Exception|bool|array
    {
        $pdo = $this->getPdo();
        $pdo->beginTransaction();
        try {
            $reference = null;
            switch ($op) {
                case "insert":
                    foreach ($requestAdds as $requestAdd) {
                        $title=$requestAdd["brandName"];
                        $description= $requestAdd["brandDescription"] ?? "";

                        $reference = $this->updateData("dm_references", ["title" => $title , "description" => $description], ["ID" => $requestAdd["lastId"]]);
                    }
                    if ($reference) {
                        $pdo->commit();
                        return true;
                    } else {
                        $pdo->rollBack();
                        return false;
                    }
                case "update";
//                        $references = $this->getData("dm_references");
//                        $reference = $this->updateData("dm_references", [ucwords($fileToWhere) => $uploaded["path"], "updated_at" => date("Y-m-d H:i:s")], ["Lang" => $file_language]);
                    if ($reference) {
                        $pdo->commit();
                        return true;
                    } else {
                        $pdo->rollBack();
                        return false;
                    }
                    break;
                default:
                    $pdo->rollBack();
                    return false;

            }


        } catch (Exception $exception) {
            $pdo->rollBack();
            http_response_code(500);
            return $exception;
        }
    }


}