use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncJob {
    pub id: i32,
    pub job_type: String,
    pub sync_direction: String,
    pub status: String,
    pub records_processed: i32,
    pub records_failed: i32,
    pub created_at: String,
    pub started_at: Option<String>,
    pub completed_at: Option<String>,
    pub error_message: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct SyncStartRequest {
    pub job_type: String,
    pub sync_direction: String,
    #[serde(default)]
    pub session_id: Option<String>,
    #[serde(default)]
    pub plesk_server_id: Option<i32>,
    #[serde(default)]
    pub sap_server_id: Option<i32>,
}

#[derive(Debug, Serialize)]
pub struct SyncStatus {
    pub is_running: bool,
    pub current_job: Option<SyncJob>,
    pub recent_jobs: Vec<SyncJob>,
    pub stats: SyncStats,
}

#[derive(Debug, Serialize)]
pub struct SyncStats {
    pub running: i64,
    pub completed_today: i64,
    pub failed_today: i64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SyncItem {
    pub id: String,
    pub source_id: String,
    pub target_id: Option<String>,
    pub name: String,
    pub status: String,
    pub source_data: serde_json::Value,
    pub target_data: Option<serde_json::Value>,
    pub diff: Option<serde_json::Value>,
}

#[derive(Debug, Serialize)]
pub struct SimulationResult {
    pub data_type: String,
    pub direction: String,
    pub total_records: usize,
    pub new: usize,
    pub updated: usize,
    pub conflicts: usize,
    pub unchanged: usize,
    pub deleted: usize,
    pub items: Vec<SyncItem>,
}

#[derive(Debug, Serialize)]
pub struct Conflict {
    pub id: i32,
    pub sync_job_id: i32,
    pub entity_type: String,
    pub entity_id: String,
    pub resolution_status: String,
    pub source_data: String,
    pub target_data: Option<String>,
    pub conflict_details: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct SimulationRequest {
    #[serde(default = "default_data_type")]
    pub data_type: String,
    #[serde(default)]
    pub direction: Option<String>,
}

fn default_data_type() -> String {
    "customers".to_string()
}

#[derive(Debug, Deserialize)]
pub struct ConflictResolution {
    pub id: i32,
    pub action: String,
    pub resolved_data: serde_json::Value,
}
