use anyhow::{Context, Result};
use std::path::Path;
use tracing::{info, warn};

use crate::core::db::{Database, PostgresDb, VideoRecord, VideoStatus};
use crate::core::probe;
use crate::core::storage::FileManager;
use crate::uuid as uuid_utils;

/// Handles the automatic ingestion of video files.
/// This service is responsible for:
/// 1. Running `ffprobe` (Pre-processing)
/// 2. Saving probe JSON
/// 3. Registering the video in the database (making it visible in the API)
pub struct IngestionService {
    db: PostgresDb,
}

impl IngestionService {
    pub fn new(db: PostgresDb) -> Self {
        Self { db }
    }

    /// Registers a video file found in the watched directory.
    /// This function is idempotent: if the video (UUID) already exists, it skips.
    pub async fn ingest(&self, file_path: &str) -> Result<Option<String>> {
        let path = Path::new(file_path);

        // 1. Validate extension
        if !is_video_extension(path) {
            return Ok(None);
        }

        // 2. Compute UUID
        let uuid = uuid_utils::compute_uuid_from_path(file_path);

        // 3. Check if already registered
        if let Ok(Some(_)) = self.db.get_video_by_uuid(&uuid).await {
            info!(
                "Video already registered: {} ({})",
                path.file_name().unwrap_or_default().to_string_lossy(),
                uuid
            );
            return Ok(None);
        }

        info!("Starting ingestion for: {} ({})", path.display(), uuid);

        // 4. Run ffprobe
        let probe_result = probe::probe_video(file_path)
            .with_context(|| format!("Failed to probe video: {}", file_path))?;

        // 5. Extract metadata
        let duration = probe_result
            .format
            .duration
            .as_ref()
            .and_then(|s| s.parse::<f64>().ok())
            .unwrap_or(0.0);

        let mut width = 0u32;
        let mut height = 0u32;
        let mut fps = 0.0;

        for stream in &probe_result.streams {
            if stream.codec_type.as_deref() == Some("video") {
                width = stream.width.unwrap_or(0);
                height = stream.height.unwrap_or(0);
                if let Some(fps_str) = &stream.r_frame_rate {
                    if let Some((num, den)) = fps_str.split_once('/') {
                        if let (Ok(n), Ok(d)) = (num.parse::<f64>(), den.parse::<f64>()) {
                            if d > 0.0 {
                                fps = n / d;
                            }
                        }
                    }
                }
            }
        }

        // 6. Save Probe JSON
        let file_manager = FileManager::new(std::path::PathBuf::from("."));
        let probe_json_str = serde_json::to_string_pretty(&probe_result)?;

        if let Err(e) = file_manager.save_json(&uuid, "probe", &probe_json_str) {
            warn!("Failed to save probe JSON for {}: {}", uuid, e);
        } else {
            info!("Probe JSON saved for {}", uuid);
        }

        // 7. Create Record
        // Use absolute path for safety
        let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        let record = VideoRecord {
            id: 0,
            uuid: uuid.clone(),
            file_path: canonical_path.to_string_lossy().to_string(),
            file_name: path
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string(),
            duration,
            width,
            height,
            fps,
            probe_json: Some(probe_json_str),
            storage: Default::default(),
            status: VideoStatus::Pending, // Ready for processing
            user_id: None,
            job_id: None,
            created_at: String::new(),
            registration_time: None,
        };

        // 8. Insert DB
        self.db
            .register_video(&record)
            .await
            .with_context(|| "Failed to register video in database")?;

        self.db
            .set_registration_time(&uuid)
            .await
            .with_context(|| "Failed to set registration_time")?;

        info!(
            "Successfully registered video: {} (UUID: {})",
            record.file_name, uuid
        );
        Ok(Some(uuid))
    }
}

fn is_video_extension(path: &Path) -> bool {
    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
        let ext = ext.to_lowercase();
        matches!(ext.as_str(), "mp4" | "mov" | "mkv" | "avi" | "webm" | "m4v")
    } else {
        false
    }
}
