#![cfg_attr(not(target_os = "macos"), allow(dead_code))]

mod a2ui;
mod a2ui_validation;
mod agent;
mod app;
mod compaction;
mod config;
mod database;
mod dev_brain;
mod dsml;
mod engine;
mod instructions;
mod metrics;
mod model;
#[cfg(target_os = "macos")]
mod native_edit;
#[cfg(target_os = "macos")]
mod native_media;
#[cfg(target_os = "macos")]
mod native_menu;
#[cfg(target_os = "macos")]
mod runtime;
mod schema;
#[cfg(target_os = "macos")]
mod server;
mod settings;

use app::{App, Message, app_icon, app_theme};
use iced::{Point, Size, window};

fn main() -> iced::Result {
    if std::env::args().nth(1).as_deref() == Some("validate-a2ui") {
        if let Err(error) = a2ui_validation::run(std::env::args().skip(2)) {
            eprintln!("A2UI validation failed: {error}");
            std::process::exit(1);
        }
        return Ok(());
    }
    agent::initialize_shell_environment();
    #[cfg(target_os = "macos")]
    if let Err(error) = engine::configure_metal_sources() {
        eprintln!("DS4Server: {error}");
    }
    iced::daemon(
        || {
            let interface = config::Config::load(&app::config_path())
                .unwrap_or_default()
                .interface;
            let (main_window, open) = window::open(window::Settings {
                size: Size::new(
                    interface.window_size[0] as f32,
                    interface.window_size[1] as f32,
                ),
                position: interface
                    .window_position
                    .map_or(window::Position::Default, |[x, y]| {
                        window::Position::Specific(Point::new(x as f32, y as f32))
                    }),
                min_size: Some(Size::new(760.0, 480.0)),
                icon: Some(app_icon()),
                exit_on_close_request: false,
                // The title bar is ours: content runs to the top edge and the
                // native traffic lights float over it.
                #[cfg(target_os = "macos")]
                platform_specific: window::settings::PlatformSpecific {
                    title_hidden: true,
                    titlebar_transparent: true,
                    fullsize_content_view: true,
                },
                ..Default::default()
            });
            (App::load(main_window), open.map(Message::WindowOpened))
        },
        App::update,
        App::view,
    )
    .title(App::title)
    .subscription(App::subscription)
    .theme(app_theme())
    .run()
}
