//! Integration tests for [`FsWindowStateStore`] against a real temp directory.

use std::path::PathBuf;
use std::sync::Arc;

use domain::layout::{PersistedWindowKind, PersistedWindowState, WindowStateSnapshot};
use domain::ports::{FileSystem, WindowStateStore};
use infrastructure::{FsWindowStateStore, LocalFileSystem};
use uuid::Uuid;

struct TempDir(PathBuf);

impl TempDir {
    fn new() -> Self {
        let path = std::env::temp_dir().join(format!("idea-window-store-{}", Uuid::new_v4()));
        std::fs::create_dir_all(&path).unwrap();
        Self(path)
    }

    fn app_data_dir(&self) -> String {
        self.0.to_string_lossy().into_owned()
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

#[tokio::test]
async fn window_state_save_then_load_roundtrips() {
    let tmp = TempDir::new();
    let fs: Arc<dyn FileSystem> = Arc::new(LocalFileSystem::new());
    let store = FsWindowStateStore::new(fs, tmp.app_data_dir());

    assert_eq!(
        store.load_window_state().await.unwrap(),
        WindowStateSnapshot::default()
    );

    let snapshot = WindowStateSnapshot::new(vec![PersistedWindowState {
        label: "main".to_owned(),
        kind: PersistedWindowKind::Main,
        panel: None,
        project_id: None,
        url: None,
        visible: true,
        maximized: true,
        fullscreen: false,
        outer_position: None,
        outer_size: None,
        monitor: None,
        last_focused_at: None,
    }]);

    store.save_window_state(&snapshot).await.unwrap();
    assert_eq!(store.load_window_state().await.unwrap(), snapshot);
    assert!(tmp.0.join("windows.json").exists());
}
