<?php

namespace Modules\Attendance\Controllers;

use App\Controllers\BaseController;
use App\Models\AttendanceCorrectionModel;
use App\Models\AttendanceRecordModel;
use Modules\Attendance\Services\AttendanceCorrectionService;

class AttendanceCorrectionController extends BaseController
{
    protected AttendanceCorrectionModel $corrections;
    protected AttendanceRecordModel $records;
    protected AttendanceCorrectionService $service;

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->corrections = new AttendanceCorrectionModel();
        $this->records     = new AttendanceRecordModel();
        $this->service     = new AttendanceCorrectionService();
    }

    public function index()
    {
        $status = $this->request->getGet('status') ?: null;

        return view('Modules\Attendance\Views\corrections\index', array_merge($this->baseViewData(), [
            'title'       => 'Attendance Corrections',
            'corrections' => $this->corrections->getWithDetails($status),
            'status'      => $status,
        ]));
    }

    public function createForm()
    {
        $recordId = (int) ($this->request->getGet('record_id') ?: 0);
        $record   = $recordId ? $this->records->find($recordId) : null;

        return view('Modules\Attendance\Views\corrections\create', array_merge($this->baseViewData(), [
            'title'  => 'Request Correction',
            'record' => $record,
        ]));
    }

    public function store()
    {
        if (! $this->validate([
            'attendance_record_id' => 'required|integer',
            'reason'             => 'required|min_length[5]',
        ])) {
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }

        $result = $this->service->request($this->request->getPost(), (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->back()->withInput()->with('errors', $result['errors']);
        }

        return redirect()->to('/attendance/corrections')->with('success', 'Correction request submitted.');
    }

    public function approve(int $id)
    {
        $result = $this->service->approve($id, (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->back()->with('error', $result['error']);
        }

        return redirect()->back()->with('success', 'Correction approved and applied.');
    }

    public function reject(int $id)
    {
        $reason = trim((string) $this->request->getPost('rejection_reason'));
        $result = $this->service->reject($id, $reason, (int) $this->session->get('user_id'));
        if (! $result['success']) {
            return redirect()->back()->with('error', $result['error']);
        }

        return redirect()->back()->with('success', 'Correction rejected.');
    }
}
