//! Core types for the routing engine
//!
//! This module contains the fundamental data structures used throughout
//! the routing system.

use std::collections::HashMap;

use rustelo_core_types::rendering::RenderingProfile;
use serde::{Deserialize, Serialize};

use crate::routing::components::RouteComponent;

/// Navigation item that can be either a route or external URL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationItem {
    /// Display label (localized)
    pub label: String,
    /// Target path or URL
    pub target: NavigationTarget,
    /// Optional icon identifier
    pub icon: Option<String>,
    /// Menu group or section
    pub group: Option<String>,
    /// Order within group
    pub order: i32,
    /// Whether item is currently active
    pub active: bool,
    /// Additional CSS classes
    pub classes: Option<String>,
}

/// Navigation target type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
pub enum NavigationTarget {
    /// Internal route path
    Route(String),
    /// External URL
    External(String),
    /// Mailto link
    Mailto(String),
    /// Tel link
    Tel(String),
    /// Email link (alias for Mailto)
    Email(String),
    /// Phone link (alias for Tel)
    Phone(String),
}

impl NavigationTarget {
    /// Get the href attribute for this navigation target
    pub fn href(&self) -> String {
        match self {
            NavigationTarget::Route(path) => path.clone(),
            NavigationTarget::External(url) => url.clone(),
            NavigationTarget::Mailto(email) | NavigationTarget::Email(email) => {
                format!("mailto:{email}")
            }
            NavigationTarget::Tel(phone) | NavigationTarget::Phone(phone) => {
                format!("tel:{phone}")
            }
        }
    }

    /// Check if this is an external link
    pub fn is_external(&self) -> bool {
        matches!(
            self,
            NavigationTarget::External(_)
                | NavigationTarget::Mailto(_)
                | NavigationTarget::Tel(_)
                | NavigationTarget::Email(_)
                | NavigationTarget::Phone(_)
        )
    }
}

/// Route metadata for SEO and navigation
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RouteMetadata {
    pub title: Option<String>,
    pub description: Option<String>,
    pub keywords: Vec<String>,
    pub language: String,
    pub canonical_path: String,
}

/// Complete route resolution with metadata
#[derive(Debug, Clone)]
pub struct RouteResolution {
    pub path: String,
    pub language: String,
    pub component: Option<RouteComponent>,
    pub canonical_path: String,
    pub metadata: RouteMetadata,
    pub exists: bool,
    pub parameters: HashMap<String, String>,
    /// Effective rendering profile after applying route-override ->
    /// workspace-default precedence.
    pub rendering_profile: RenderingProfile,
    /// Template file name when the route is dispatched to the Minijinja engine.
    pub template: Option<String>,
}
