use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct ChurchConfig {
    pub church_name: Option<String>,
    pub church_address: Option<String>,
    pub po_box: Option<String>,
    pub contact_phone: Option<String>,
    pub google_maps_url: Option<String>,
    pub about_text: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
    pub id: String,
    pub title: String,
    pub description: Option<String>,
    pub start_time: String,
    pub end_time: String,
    pub location: String,
    pub location_url: Option<String>,
    pub category: Option<String>,
    pub image: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bulletin {
    pub id: String,
    pub title: String,
    pub date: String,
    pub scripture_reading: Option<String>,
    pub sabbath_school: Option<String>,
    pub divine_worship: Option<String>,
    pub sunset: Option<String>,
    pub pdf_path: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BibleVerse {
    pub text: String,
    pub reference: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Sermon {
    pub id: String,
    pub title: String,
    pub speaker: Option<String>,
    pub date: String,
    pub description: Option<String>,
    pub audio_url: Option<String>,
    pub video_url: Option<String>,
    pub series: Option<String>,
    pub scripture: Option<String>,
    pub duration: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ContactForm {
    pub first_name: String,
    pub last_name: Option<String>,
    pub email: String,
    #[serde(default)]
    pub phone: Option<String>,
    pub subject: String,
    pub message: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinAuth {
    #[serde(rename = "Username")]
    pub username: String,
    #[serde(rename = "Pw")]
    pub pw: String,
    #[serde(rename = "request")]
    pub request: serde_json::Value,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinAuthResponse {
    #[serde(rename = "AccessToken")]
    pub access_token: String,
    #[serde(rename = "User")]
    pub user: JellyfinUser,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinUser {
    #[serde(rename = "Id")]
    pub id: String,
    #[serde(rename = "Name")]
    pub name: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinItem {
    #[serde(rename = "Id")]
    pub id: String,
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "PremiereDate")]
    pub premiere_date: Option<String>,
    #[serde(rename = "DateCreated")]
    pub date_created: Option<String>,
    #[serde(rename = "MediaType")]
    pub media_type: Option<String>,
    #[serde(rename = "RunTimeTicks")]
    pub run_time_ticks: Option<u64>,
    #[serde(rename = "Overview")]
    pub overview: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinLibrary {
    #[serde(rename = "Id")]
    pub id: String,
    #[serde(rename = "Name")]
    pub name: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinItemsResponse {
    #[serde(rename = "Items")]
    pub items: Vec<JellyfinItem>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JellyfinLibrariesResponse {
    #[serde(rename = "Items")]
    pub items: Vec<JellyfinLibrary>,
}

#[derive(Debug, Clone)]
pub struct ParsedSermon {
    pub title: String,
    pub speaker: Option<String>,
    pub date_from_title: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    pub message: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ApiListResponse<T> {
    pub success: bool,
    pub data: ApiListData<T>,
    pub message: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ApiListData<T> {
    pub items: Vec<T>,
    pub total: u32,
    pub page: u32,
    pub per_page: u32,
    pub has_more: bool,
}

// Template-specific structs that match what templates expect
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct TemplateEvent {
    pub id: String,
    pub title: String,
    pub description: String,
    pub start_time: String,
    pub end_time: String,
    pub location: String,
    pub location_url: String,
    pub category: String,
    pub image: String,
}

impl From<Event> for TemplateEvent {
    fn from(event: Event) -> Self {
        Self {
            id: event.id,
            title: event.title,
            description: event.description.unwrap_or_default(),
            start_time: event.start_time,
            end_time: event.end_time,
            location: event.location,
            location_url: event.location_url.unwrap_or_default(),
            category: event.category.unwrap_or_default(),
            image: event.image.unwrap_or_default(),
        }
    }
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct TemplateBulletin {
    pub id: String,
    pub title: String,
    pub date: String,
    pub scripture_reading: String,
    pub sabbath_school: String,
    pub divine_worship: String,
    pub sunset: String,
    pub pdf_path: String,
}

impl From<Bulletin> for TemplateBulletin {
    fn from(bulletin: Bulletin) -> Self {
        Self {
            id: bulletin.id,
            title: bulletin.title,
            date: bulletin.date,
            scripture_reading: bulletin.scripture_reading.unwrap_or_default(),
            sabbath_school: bulletin.sabbath_school.unwrap_or_default(),
            divine_worship: bulletin.divine_worship.unwrap_or_default(),
            sunset: bulletin.sunset.unwrap_or_default(),
            pdf_path: bulletin.pdf_path.unwrap_or_default(),
        }
    }
}