use chrono::Utc;
use uuid::Uuid;

use crate::types::error::Result;
use crate::types::model::{
    PlatformAudioSpecification, PlatformBeamgramSpecification,
    PlatformBearingTimeRecordSpecification, PlatformSpectrogramSpecification,
};

use super::PostgresRepository;

impl PostgresRepository {
    /// Delete a platform-audio specification link. Returns the number of rows removed.
    pub async fn delete_platform_audio_specification(&self, id: Uuid) -> Result<u64> {
        let result = sqlx::query!(
            "DELETE FROM horizon_public.platform_audio_specification WHERE id = $1",
            id
        )
        .execute(&self.pool)
        .await?;
        Ok(result.rows_affected())
    }

    /// Delete a platform-beamgram specification link. Returns the number of rows removed.
    pub async fn delete_platform_beamgram_specification(&self, id: Uuid) -> Result<u64> {
        let result = sqlx::query!(
            "DELETE FROM horizon_public.platform_beamgram_specification WHERE id = $1",
            id
        )
        .execute(&self.pool)
        .await?;
        Ok(result.rows_affected())
    }

    /// Delete a platform-BTR specification link. Returns the number of rows removed.
    pub async fn delete_platform_bearing_time_record_specification(&self, id: Uuid) -> Result<u64> {
        let result = sqlx::query!(
            "DELETE FROM horizon_public.platform_bearing_time_record_specification WHERE id = $1",
            id
        )
        .execute(&self.pool)
        .await?;
        Ok(result.rows_affected())
    }

    /// Delete a platform-spectrogram specification link. Returns the number of rows removed.
    pub async fn delete_platform_spectrogram_specification(&self, id: Uuid) -> Result<u64> {
        let result = sqlx::query!(
            "DELETE FROM horizon_public.platform_spectrogram_specification WHERE id = $1",
            id
        )
        .execute(&self.pool)
        .await?;
        Ok(result.rows_affected())
    }

    /// Create a new platform-audio specification link.
    pub async fn insert_platform_audio_specification(
        &self,
        j: &PlatformAudioSpecification,
    ) -> Result<PlatformAudioSpecification> {
        let now = Utc::now();
        let org = self.organization_id.or(j.organization_id);
        Ok(sqlx::query_as!(
            PlatformAudioSpecification,
            r#"
            INSERT INTO horizon_public.platform_audio_specification
                (id, platform_id, audio_specification_id, organization_id, modified_datetime)
            VALUES (COALESCE($1, gen_random_uuid()), $2, $3, $4, $5)
            RETURNING id, created_datetime, modified_datetime,
                      platform_id, audio_specification_id, organization_id
            "#,
            j.id,
            j.platform_id,
            j.audio_specification_id,
            org,
            now,
        )
        .fetch_one(&self.pool)
        .await?)
    }

    /// Create a new platform-beamgram specification link.
    pub async fn insert_platform_beamgram_specification(
        &self,
        j: &PlatformBeamgramSpecification,
    ) -> Result<PlatformBeamgramSpecification> {
        let now = Utc::now();
        let org = self.organization_id.or(j.organization_id);
        Ok(sqlx::query_as!(
            PlatformBeamgramSpecification,
            r#"
            INSERT INTO horizon_public.platform_beamgram_specification
                (id, platform_id, beamgram_specification_id, organization_id, modified_datetime)
            VALUES (COALESCE($1, gen_random_uuid()), $2, $3, $4, $5)
            RETURNING id, created_datetime, modified_datetime,
                      platform_id, beamgram_specification_id, organization_id
            "#,
            j.id,
            j.platform_id,
            j.beamgram_specification_id,
            org,
            now,
        )
        .fetch_one(&self.pool)
        .await?)
    }

    /// Create a new platform-BTR specification link.
    pub async fn insert_platform_bearing_time_record_specification(
        &self,
        j: &PlatformBearingTimeRecordSpecification,
    ) -> Result<PlatformBearingTimeRecordSpecification> {
        let now = Utc::now();
        let org = self.organization_id.or(j.organization_id);
        Ok(sqlx::query_as!(
            PlatformBearingTimeRecordSpecification,
            r#"
            INSERT INTO horizon_public.platform_bearing_time_record_specification
                (id, platform_id, bearing_time_record_specification_id,
                 organization_id, modified_datetime)
            VALUES (COALESCE($1, gen_random_uuid()), $2, $3, $4, $5)
            RETURNING id, created_datetime, modified_datetime,
                      platform_id, bearing_time_record_specification_id, organization_id
            "#,
            j.id,
            j.platform_id,
            j.bearing_time_record_specification_id,
            org,
            now,
        )
        .fetch_one(&self.pool)
        .await?)
    }

    /// Create a new platform-spectrogram specification link.
    pub async fn insert_platform_spectrogram_specification(
        &self,
        j: &PlatformSpectrogramSpecification,
    ) -> Result<PlatformSpectrogramSpecification> {
        let now = Utc::now();
        let org = self.organization_id.or(j.organization_id);
        Ok(sqlx::query_as!(
            PlatformSpectrogramSpecification,
            r#"
            INSERT INTO horizon_public.platform_spectrogram_specification
                (id, platform_id, spectrogram_specification_id,
                 organization_id, modified_datetime)
            VALUES (COALESCE($1, gen_random_uuid()), $2, $3, $4, $5)
            RETURNING id, created_datetime, modified_datetime,
                      platform_id, spectrogram_specification_id, organization_id
            "#,
            j.id,
            j.platform_id,
            j.spectrogram_specification_id,
            org,
            now,
        )
        .fetch_one(&self.pool)
        .await?)
    }

    /// List all platform-audio specification link records.
    pub async fn list_platform_audio_specifications(
        &self,
    ) -> Result<Vec<PlatformAudioSpecification>> {
        Ok(sqlx::query_as!(
            PlatformAudioSpecification,
            r#"
            SELECT id, created_datetime, modified_datetime,
                   platform_id, audio_specification_id, organization_id
            FROM horizon_public.platform_audio_specification
            "#,
        )
        .fetch_all(&self.pool)
        .await?)
    }

    /// List all platform-beamgram specification link records.
    pub async fn list_platform_beamgram_specifications(
        &self,
    ) -> Result<Vec<PlatformBeamgramSpecification>> {
        Ok(sqlx::query_as!(
            PlatformBeamgramSpecification,
            r#"
            SELECT id, created_datetime, modified_datetime,
                   platform_id, beamgram_specification_id, organization_id
            FROM horizon_public.platform_beamgram_specification
            "#,
        )
        .fetch_all(&self.pool)
        .await?)
    }

    /// List all platform-BTR specification link records.
    pub async fn list_platform_bearing_time_record_specifications(
        &self,
    ) -> Result<Vec<PlatformBearingTimeRecordSpecification>> {
        Ok(sqlx::query_as!(
            PlatformBearingTimeRecordSpecification,
            r#"
            SELECT id, created_datetime, modified_datetime,
                   platform_id, bearing_time_record_specification_id, organization_id
            FROM horizon_public.platform_bearing_time_record_specification
            "#,
        )
        .fetch_all(&self.pool)
        .await?)
    }

    /// List all platform-spectrogram specification link records.
    pub async fn list_platform_spectrogram_specifications(
        &self,
    ) -> Result<Vec<PlatformSpectrogramSpecification>> {
        Ok(sqlx::query_as!(
            PlatformSpectrogramSpecification,
            r#"
            SELECT id, created_datetime, modified_datetime,
                   platform_id, spectrogram_specification_id, organization_id
            FROM horizon_public.platform_spectrogram_specification
            "#,
        )
        .fetch_all(&self.pool)
        .await?)
    }
}
