use oiseau::cache::Cache;
use crate::model::id::Id;
use crate::model::moderation::AuditLogEntry;
use crate::model::{Error, Result, auth::User, posts::PostDraft, permissions::FinePermission};
use crate::{auto_method, DataManager};

use oiseau::PostgresRow;

use oiseau::{execute, get, query_rows, params};

impl DataManager {
    /// Get a [`PostDraft`] from an SQL row.
    pub(crate) fn get_draft_from_row(x: &PostgresRow) -> PostDraft {
        PostDraft {
            id: Id::deserialize(&get!(x->0(String))),
            created: get!(x->1(i64)) as u128,
            content: get!(x->2(String)),
            owner: Id::deserialize(&get!(x->3(String))),
        }
    }

    auto_method!(get_draft_by_id()@get_draft_from_row -> "SELECT * FROM drafts WHERE id = $1" --name="draft" --returns=PostDraft --cache-key-tmpl="atto.draft:{}");

    /// Get all drafts from the given user (from most recent, paginated).
    ///
    /// # Arguments
    /// * `id` - the ID of the user the requested drafts belong to
    /// * `batch` - the limit of posts in each page
    /// * `page` - the page number
    pub async fn get_drafts_by_user(
        &self,
        id: &Id,
        batch: usize,
        page: usize,
    ) -> Result<Vec<PostDraft>> {
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        // ...
        let res = query_rows!(
            &conn,
            "SELECT * FROM drafts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
            &[&id.printable(), &(batch as i64), &((page * batch) as i64)],
            |x| { Self::get_draft_from_row(x) }
        );

        if res.is_err() {
            return Err(Error::GeneralNotFound("draft".to_string()));
        }

        Ok(res.unwrap())
    }

    /// Get all drafts from the given user (from most recent).
    ///
    /// # Arguments
    /// * `id` - the ID of the user the requested drafts belong to
    pub async fn get_drafts_by_user_all(&self, id: &Id) -> Result<Vec<PostDraft>> {
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        // ...
        let res = query_rows!(
            &conn,
            "SELECT * FROM drafts WHERE owner = $1 ORDER BY created DESC",
            &[&id.printable()],
            |x| { Self::get_draft_from_row(x) }
        );

        if res.is_err() {
            return Err(Error::GeneralNotFound("draft".to_string()));
        }

        Ok(res.unwrap())
    }

    const MAXIMUM_FREE_DRAFTS: usize = 10;

    /// Create a new post draft in the database.
    ///
    /// # Arguments
    /// * `data` - a mock [`PostDraft`] object to insert
    pub async fn create_draft(&self, data: PostDraft) -> Result<Id> {
        // check values
        if data.content.len() < 2 {
            return Err(Error::DataTooShort("content".to_string()));
        } else if data.content.len() > 4096 {
            return Err(Error::DataTooLong("content".to_string()));
        }

        // check number of stacks
        let owner = self.get_user_by_id(&data.owner).await?;

        if !owner.permissions.check(FinePermission::Supporter) {
            let drafts = self.get_drafts_by_user_all(&data.owner).await?;

            if drafts.len() >= Self::MAXIMUM_FREE_DRAFTS {
                return Err(Error::MiscError(
                    "You already have the maximum number of drafts you can have".to_string(),
                ));
            }
        }

        // ...
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = execute!(
            &conn,
            "INSERT INTO drafts VALUES ($1, $2, $3, $4)",
            params![
                &data.id.printable(),
                &(data.created as i64),
                &data.content,
                &data.owner.printable(),
            ]
        );

        if let Err(e) = res {
            return Err(Error::DatabaseError(e.to_string()));
        }

        Ok(data.id)
    }

    pub async fn delete_draft(&self, id: &crate::model::id::Id, user: User) -> Result<()> {
        let y = self.get_draft_by_id(id).await?;

        if user.id != y.owner {
            if !user.permissions.check(FinePermission::ManagePosts) {
                return Err(Error::NotAllowed);
            } else {
                self.create_audit_log_entry(AuditLogEntry::new(
                    user.id,
                    format!("invoked `delete_draft` with x value `{id}`"),
                ))
                .await?
            }
        }
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = execute!(
            &conn,
            "DELETE FROM drafts WHERE id = $1",
            &[&id.printable()]
        );

        if let Err(e) = res {
            return Err(Error::DatabaseError(e.to_string()));
        }

        self.0.1.remove(format!("atto.draft:{}", id)).await;

        Ok(())
    }

    pub async fn update_draft_content(
        &self,
        id: &crate::model::id::Id,
        user: User,
        x: String,
    ) -> Result<()> {
        let y = self.get_draft_by_id(id).await?;

        if user.id != y.owner {
            if !user.permissions.check(FinePermission::ManagePosts) {
                return Err(Error::NotAllowed);
            } else {
                self.create_audit_log_entry(AuditLogEntry::new(
                    user.id,
                    format!("invoked `update_draft_content` with x value `{id}`"),
                ))
                .await?
            }
        }

        // ...
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = execute!(
            &conn,
            "UPDATE drafts SET content = $1 WHERE id = $2",
            params![&x, &id.printable()]
        );

        if let Err(e) = res {
            return Err(Error::DatabaseError(e.to_string()));
        }

        Ok(())
    }
}
