// src/state/pages/auth.rs

use canvas::{DataProvider, AppMode};

/// Strongly typed user roles
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UserRole {
    Admin,
    Moderator,
    Accountant,
    Viewer,
    Unknown(String), // fallback for unexpected roles
}

impl UserRole {
    pub fn from_str(s: &str) -> Self {
        match s.trim().to_lowercase().as_str() {
            "admin" => UserRole::Admin,
            "moderator" => UserRole::Moderator,
            "accountant" => UserRole::Accountant,
            "viewer" => UserRole::Viewer,
            other => UserRole::Unknown(other.to_string()),
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            UserRole::Admin => "admin",
            UserRole::Moderator => "moderator",
            UserRole::Accountant => "accountant",
            UserRole::Viewer => "viewer",
            UserRole::Unknown(s) => s.as_str(),
        }
    }
}

/// Represents the authenticated session state
#[derive(Default)]
pub struct AuthState {
    pub auth_token: Option<String>,
    pub user_id: Option<String>,
    pub role: Option<UserRole>,
    pub decoded_username: Option<String>,
}

impl AuthState {
    pub fn new() -> Self {
        Self::default()
    }
}
