use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;

use super::{BaseFields, TaskFrequency};

// ==================== SERVICE SCOPES ====================

/// Service scope assigned to an account address (location)
/// Exactly ONE active scope per account address
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServiceScope {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub name: String,
    pub account_id: Uuid,
    pub account_address_id: Uuid,
    pub description: Option<String>,
    pub is_active: bool,
}

/// Area within a service scope (sub-division)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServiceScopeArea {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub scope_id: Uuid,
    pub name: String,
    pub order: i32,
}

/// Task within a service scope area
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServiceScopeTask {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub area_id: Uuid,
    /// Customer-facing description
    pub scope_description: String,
    /// QA/punchlist formatted description
    pub checklist_description: String,
    /// Team member work instructions (shown during sessions)
    pub session_description: String,
    pub frequency: TaskFrequency,
    pub order: i32,
    pub estimated_minutes: Option<i32>,
}

// ==================== PROJECT SCOPES ====================

/// Scope for project work (assigned directly to project, not location-based)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectScope {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub name: String,
    pub project_id: Uuid,
    /// Optional: if project is at a specific location
    pub account_id: Option<Uuid>,
    pub account_address_id: Option<Uuid>,
    pub description: Option<String>,
    pub is_active: bool,
}

/// Category within a project scope (equivalent to Area for services)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectScopeCategory {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub scope_id: Uuid,
    pub name: String,
    pub order: i32,
}

/// Task within a project scope category
/// Note: Unlike service tasks, project tasks don't have frequency - they are one-time
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectScopeTask {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub category_id: Uuid,
    /// Customer-facing description
    pub scope_description: String,
    /// QA/punchlist formatted description
    pub checklist_description: String,
    /// Team member work instructions (shown during sessions)
    pub session_description: String,
    pub order: i32,
    pub estimated_minutes: Option<i32>,
}

// ==================== SERVICE SCOPE TEMPLATES ====================

/// Reusable service scope template (instantiated as ServiceScope)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServiceScopeTemplate {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub name: String,
    pub description: Option<String>,
    pub is_active: bool,
}

/// Area within a service scope template
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServiceScopeTemplateArea {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub template_id: Uuid,
    pub name: String,
    pub order: i32,
}

/// Task within a service scope template area
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ServiceScopeTemplateTask {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub area_id: Uuid,
    /// Customer-facing description
    pub scope_description: String,
    /// QA/punchlist formatted description
    pub checklist_description: String,
    /// Team member work instructions (shown during sessions)
    pub session_description: String,
    pub frequency: TaskFrequency,
    pub order: i32,
    pub estimated_minutes: Option<i32>,
}

// ==================== PROJECT SCOPE TEMPLATES ====================

/// Reusable project scope template (instantiated as ProjectScope)
/// Unlike service templates, project templates don't have frequency on tasks
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectScopeTemplate {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub name: String,
    pub description: Option<String>,
    pub is_active: bool,
}

/// Category within a project scope template (equivalent to Area)
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectScopeTemplateCategory {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub template_id: Uuid,
    pub name: String,
    pub order: i32,
}

/// Task within a project scope template category
/// Note: No frequency field - project tasks are one-time
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ProjectScopeTemplateTask {
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub base: BaseFields,
    pub category_id: Uuid,
    /// Customer-facing description
    pub scope_description: String,
    /// QA/punchlist formatted description
    pub checklist_description: String,
    /// Team member work instructions (shown during sessions)
    pub session_description: String,
    pub order: i32,
    pub estimated_minutes: Option<i32>,
}
