use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;

use super::BaseFields;

// ==================== SERVICE PUNCHLISTS ====================

/// Service punchlist (customer-facing snapshot from completed session)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServicePunchlist {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub session_id: Uuid,
    /// Denormalized for easy access
    pub customer_id: Uuid,
    pub account_id: Uuid,
    pub account_address_id: Uuid,
    pub date: NaiveDate,
    /// URL to exported PDF
    pub pdf_url: Option<String>,
    /// When the punchlist was exported to PDF
    pub exported_at: Option<DateTime<Utc>>,
    pub notes: Option<String>,
}

/// Individual task entry on a service punchlist (snapshot of scope task)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServicePunchlistItem {
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub punchlist_id: Uuid,
    /// Reference to original task (nullable if task was deleted)
    pub task_id: Option<Uuid>,
    /// Snapshot of checklist_description at time of punchlist creation
    pub checklist_description: String,
    pub order: i32,
    pub is_completed: bool,
    pub completed_at: Option<DateTime<Utc>>,
    pub completed_by_id: Option<Uuid>,
}

// ==================== PROJECT PUNCHLISTS ====================

/// Project punchlist (customer-facing snapshot from completed session)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectPunchlist {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub session_id: Uuid,
    /// Denormalized for easy access
    pub customer_id: Uuid,
    pub project_id: Uuid,
    pub date: NaiveDate,
    /// URL to exported PDF
    pub pdf_url: Option<String>,
    /// When the punchlist was exported to PDF
    pub exported_at: Option<DateTime<Utc>>,
    pub notes: Option<String>,
}

/// Individual task entry on a project punchlist (snapshot of scope task)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectPunchlistItem {
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub punchlist_id: Uuid,
    /// Reference to original task (nullable if task was deleted)
    pub task_id: Option<Uuid>,
    /// Snapshot of checklist_description at time of punchlist creation
    pub checklist_description: String,
    pub order: i32,
    pub is_completed: bool,
    pub completed_at: Option<DateTime<Utc>>,
    pub completed_by_id: Option<Uuid>,
}
