use rocket::form::validate::Contains;
use serde::{Deserialize, Serialize};

use crate::db;

use super::{course::Course, custom_label::CustomLabel};

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(crate = "rocket::serde")]
/// Represents a single register send by the frontend
/// to create a new register in the database
pub struct RegisterCreate {
    person_id: i32,
    course_id: i32,
    /// YYYY-MM-DD
    date: String,
    /// Foreign key to the custom_label table
    custom_label: String,
    is_preview: bool,
}

impl RegisterCreate {
    pub async fn create(&self) -> Result<(), sqlx::Error> {
        let db = db();

        // Get custom_label_id from db based of self.custom_label
        let custom_label_id = {
            if self.custom_label.is_empty() {
                1
            } else {
                // Get custom_label_id from db based of self.custom_label
                let id = CustomLabel::get_id_by_value(&self.custom_label).await?;

                if id > 0 {
                    id
                } else {
                    CustomLabel::create(&self.custom_label).await?
                }
            }
        };

        let next_register_code = Self::get_next_register_code(self.course_id).await?;

        // Current date in YYYY-MM-DD format
        let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();

        let _ = sqlx::query!(
            "INSERT INTO register (
                register_code,
                register_creation_date,
                register_display_date,
                register_custom_label,
                register_is_preview,
                register_person_id,
                register_course_id
            ) VALUES (?, ?, ?, ?, ?, ?, ?)",
            next_register_code,
            current_date,
            self.date,
            custom_label_id,
            self.is_preview,
            self.person_id,
            self.course_id
        )
        .execute(db)
        .await?;

        Ok(())
    }

    async fn get_next_register_code(course_id: i32) -> Result<i32, sqlx::Error> {
        let db = db();

        let course_name = Course::get_course_name(course_id).await;

        // All matpel certs share codes
        let new_max = if course_name.contains("Matpel") {
            let res = sqlx::query!(
                "SELECT MAX(register_code) AS max FROM register 
                WHERE register_course_id IN 
                (SELECT course_id FROM course WHERE course_name LIKE 'Matpel%')",
            )
            .fetch_one(db)
            .await?;

            // If there are no registers, the new code will start from 50, as per the requirements
            res.max.unwrap_or(50) + 1
        } else {
            let res = sqlx::query!(
                "SELECT MAX(register_code) AS max FROM register
                WHERE register_course_id = ?",
                course_id
            )
            .fetch_one(db)
            .await?;

            // If there are no registers, the new code will start from 50, as per the requirements
            res.max.unwrap_or(50) + 1
        };

        Ok(new_max)
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Register {
    pub register_id: i32,
    /// Display code, shown on the certificate.
    /// Currently a 4-digit number. After 9999, it will be a 4-digit hex number.
    ///
    /// Example: `0322``
    pub register_code: i32,
    /// Date of creation of the register, in YYYY-MM-DD format
    pub register_creation_date: String,
    /// Date shown on the certificate, in YYYY-MM-DD format
    pub register_display_date: String,
    /// Some course's names can be extended with a label.
    /// Points to a custom_label entity
    pub register_custom_label: i32,
    /// Whether this register is a preview of the certificate ("sin firmas")
    pub register_is_preview: bool,
    /// Foreign key to the person table
    pub register_person_id: i32,
    /// Foreign key to the course table
    pub register_course_id: i32,
}

impl Register {
    pub async fn get_by_dni(dni: i32) -> Result<Vec<Register>, sqlx::Error> {
        let db = db();

        let res = sqlx::query!(
            "SELECT * FROM register 
                WHERE register_person_id = (SELECT person_id FROM person WHERE person_dni = ?)",
            dni
        )
        .fetch_all(db)
        .await?
        .into_iter()
        .map(|r| Register {
            register_id: r.register_id,
            register_code: r.register_code,
            register_creation_date: r.register_creation_date.format("%Y-%m-%d").to_string(),
            register_display_date: r.register_display_date.format("%Y-%m-%d").to_string(),
            register_custom_label: r.register_custom_label,
            register_is_preview: r.register_is_preview == 1,
            register_person_id: r.register_person_id,
            register_course_id: r.register_course_id,
        })
        .collect();

        Ok(res)
    }

    pub async fn delete(register_id: i32) -> Result<(), sqlx::Error> {
        let db = db();

        let _ = sqlx::query!("DELETE FROM register WHERE register_id = ?", register_id)
            .execute(db)
            .await?;

        Ok(())
    }
}
