<?php

namespace App\Controllers\Admin;

use App\Models\Perfex\PerfexProjectsTasksModel;

class ProjetsPerfex extends \App\Controllers\BaseController
{
    private PerfexProjectsTasksModel $model;

    public function __construct()
    {
        $this->model = new PerfexProjectsTasksModel();
    }

    public function promos()
    {
        return $this->renderProjectTasksPage(
            6,
            'Promos',
            'Tâches du projet Perfex #6'
        );
    }

    public function lancements()
    {
        return $this->renderProjectTasksPage(
            4,
            'Lancements',
            'Tâches du projet Perfex #4'
        );
    }

    private function renderProjectTasksPage(int $projectId, string $title, string $subtitle)
    {
        try {
            $project = $this->model->getProjectMeta($projectId);
            $tasks = $this->model->getTasksByProjectId($projectId);

            return view('admin/projetsperfex/index', [
                'title' => $title,
                'subtitle' => $subtitle,
                'projectId' => $projectId,
                'project' => $project,
                'tasks' => $tasks,
                'errorMessage' => null,
            ]);
        } catch (\Throwable $e) {
            return view('admin/projetsperfex/index', [
                'title' => $title,
                'subtitle' => $subtitle,
                'projectId' => $projectId,
                'project' => [],
                'tasks' => [],
                'errorMessage' => $e->getMessage(),
            ]);
        }
    }
}
