<?php

namespace App\Controllers;


use App\Models\ClassAttendanceModel;
use App\Models\ClassDayModel;
use App\Models\PaymentModel;
use App\Models\PaymentMethodModel;
use App\Models\ClassModel;
use App\Models\ClassStatusModel;
use App\Models\ClassStudentModel;
use App\Models\LessonModel;
use App\Models\StudentModel;
use App\Models\ClassDiaryModel;

class Payment extends BaseController
{
    public function index()
    {
        $model = new PaymentModel();
        $studentModel = new StudentModel();
        $paymentMethodModel = new PaymentMethodModel();

        $data['students'] = $studentModel->whereIn('status', [1])->orderBy('first_name', 'ASC')->findAll();
        $data['paymentmethods'] = $paymentMethodModel->whereIn('status', [1])->orderBy('created_datetime', 'DESC')->findAll();

        return view('payment', $data);
    }

    public function get($id)
    {
        $paymentModel = new PaymentModel();

        $data = $paymentModel
            ->select("
                payment.id,
                CONCAT(
                    DATE_FORMAT(class_diary.date, '%Y-%m-%d'),
                    ' (',
                    TIME_FORMAT(class_diary.started_time, '%h:%i %p'),
                    ' - ',
                    TIME_FORMAT(class_diary.ended_time, '%h:%i %p'),
                    ')'
                ) AS class_time,
                class.name AS class_name,
                CONCAT(student.first_name, ' ', student.last_name) AS student_name,
                class_student.fee AS student_fee,
                payment.status,
                payment.paid_date,
                payment_method.name AS payment_method_name
            ")
            ->join('class_diary', 'class_diary.id = payment.class_diary_id')
            ->join('class', 'class.id = class_diary.class_id')
            ->join('student', 'student.id = payment.student_id')
            ->join('class_student', 'class_student.class_id = class_diary.class_id AND class_student.student_id = payment.student_id')
            ->join('payment_method', 'payment_method.id = payment.payment_method_id', 'left')
            ->where('payment.id', $id)
            ->findAll();

        return $this->response->setJSON($data);
    }

    public function getPaymentByStudentId($studentId)
    {
        $paymentModel = new PaymentModel();

        $data = $paymentModel
            ->select("
                payment.id,
                CONCAT(
                    DATE_FORMAT(class_diary.date, '%Y-%m-%d'),
                    ' (',
                    TIME_FORMAT(class_diary.started_time, '%h:%i %p'),
                    ' - ',
                    TIME_FORMAT(class_diary.ended_time, '%h:%i %p'),
                    ')'
                ) AS class_time,
                class.name AS class_name,
                CONCAT(student.first_name, ' ', student.last_name) AS student_name,
                payment.status,
                payment.paid_date,
                payment_method.name AS payment_method_name
            ")
            ->join('class_diary', 'class_diary.id = payment.class_diary_id')
            ->join('class', 'class.id = class_diary.class_id')
            ->join('student', 'student.id = payment.student_id')
            ->join('payment_method', 'payment_method.id = payment.payment_method_id', 'left')
            ->where('payment.student_id', $studentId)
            ->orderBy('class_diary.date', 'DESC')
            ->findAll();

        return $this->response->setJSON([
            "data" => $data
        ]);

    }

    public function update()
    {
        $paymentModel = new PaymentModel();

        $id = $this->request->getPost('updid');

        $data = [
            'paid_date' => $this->request->getPost('updpaidDate'),
            'payment_method_id' => $this->request->getPost('payment_method_id'),
            'fee' => $this->request->getPost('updfee'),
            'status' => 1
        ];

        if ($paymentModel->update($id, $data)) {

            return $this->response->setJSON([
                'status' => 'success',
                'message' => 'Payment updated successfully.'
            ]);

        }

        return $this->response->setJSON([
            'status' => 'error',
            'message' => 'Failed to update payment.'
        ]);
    }

    public function getAttendance($id)
    {
        $model = new classAttendanceModel();

        $data = $model->getAttendanceSummary($id);

        return $this->response->setJSON($data);
    }

    public function getPendingFeeCount($id)
    {
        $model = new PaymentModel();

        $data = $model->getPendingFeeCount($id);

        return $this->response->setJSON($data);
    }
}