<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\AcademicYearModel;

class AcademicYear extends BaseController
{
    protected $model;

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

    private function checkLogin()
    {
        if (!session()->get('logged_in')) {
            return redirect()->to('/login');
        }
        return null;
    }

    public function index()
    {
        return redirect()->to('/academic-period');
    }

    public function store()
    {
        if ($redirect = $this->checkLogin()) return $redirect;

        $rules = [
            'name'       => 'required|max_length[20]|is_unique[academic_years.name]',
            'start_date' => 'required|valid_date',
            'end_date'   => 'required|valid_date',
        ];

        if (!$this->validate($rules)) {
            return redirect()->back()
                ->with('errors', $this->validator->getErrors())
                ->withInput();
        }

        $this->model->insert([
            'name'       => $this->request->getPost('name'),
            'start_date' => $this->request->getPost('start_date'),
            'end_date'   => $this->request->getPost('end_date'),
            'is_active'  => 0,
        ]);

        return redirect()->to('/academic-period')
            ->with('success', 'Tahun ajaran berhasil ditambahkan!');
    }

    public function update($id)
    {
        if ($redirect = $this->checkLogin()) return $redirect;

        $rules = [
            'name'       => "required|max_length[20]|is_unique[academic_years.name,id,{$id}]",
            'start_date' => 'required|valid_date',
            'end_date'   => 'required|valid_date',
        ];

        if (!$this->validate($rules)) {
            return redirect()->back()
                ->with('errors', $this->validator->getErrors())
                ->withInput();
        }

        $this->model->update($id, [
            'name'       => $this->request->getPost('name'),
            'start_date' => $this->request->getPost('start_date'),
            'end_date'   => $this->request->getPost('end_date'),
        ]);

        return redirect()->to('/academic-period')
            ->with('success', 'Tahun ajaran berhasil diupdate!');
    }

    public function setActive($id)
    {
        if ($redirect = $this->checkLogin()) return $redirect;

        $this->model->setActive($id);

        return redirect()->to('/academic-period')
            ->with('success', 'Tahun ajaran aktif berhasil diubah!');
    }

    public function delete($id)
    {
        if ($redirect = $this->checkLogin()) return $redirect;

        $year = $this->model->find($id);

        if ($year['is_active'] == 1) {
            return redirect()->to('/academic-period')
                ->with('error', 'Tahun ajaran aktif tidak bisa dihapus!');
        }

        $this->model->delete($id);

        return redirect()->to('/academic-period')
            ->with('success', 'Tahun ajaran berhasil dihapus!');
    }
}