//! atproto-static-web   A simple web viewer for AT Proto.
//! Copyright (C) 2026  AverageHelper
//!
//! This program is free software: you can redistribute it and/or modify
//! it under the terms of the GNU General Public License as published by
//! the Free Software Foundation, either version 3 of the License, or
//! (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::{
	constants::{GENERATOR, REPO_URL, USER_AGENT, WEB_COPYRIGHT_NOTICE},
	server::lang::{Lang, tr},
};
use maud::{DOCTYPE, Markup, html};
use rocket::http::Status;

pub fn error(lang: Lang, status: Status) -> Markup {
	let code = status.code;
	let reason = status.reason().unwrap_or("default");
	let desc_key = match code {
		// Keep this list up-to-date with translations
		400..=418 | 421 | 422 | 426 | 428 | 429 | 431 | 451 | 500..=504 | 510 => {
			&format!("error.{code}")
		}
		_ => "error.generic",
	};
	let description = tr!(lang, desc_key);

	html! {
		(DOCTYPE)
		(WEB_COPYRIGHT_NOTICE)
		html lang=(lang) {
			head {
				meta charset="UTF-8";
				title { (code)" "(reason) }
				link rel="icon" type="image/png" href="data:image/png;base64,";
				meta name="generator" content=(GENERATOR);
				meta name="robots" content="noai";
				meta name="viewport" content="width=device-width, initial-scale=1.0";
				meta name="color-scheme" content="light dark";
			}
			body style="text-align:center;" {
				main {
					h1 { (code)": "(reason) }
					p { (description) }
					hr;
				}
				footer {
					a rel="external noopener noreferrer" href=(&*REPO_URL) {
						span style="font-size:small;" { (USER_AGENT) }
					}
				}
			}
		}
	}
}
