//! TV API command handlers.

use crate::api::client::GdeltClient;
use crate::api::tv::{
    TvApi, TvAiApi, TvSearchParams, TvClipsParams, TvTimelineParams, TvStationsParams,
    TvAiSearchParams, TvAiConceptsParams, TvAiVisualParams,
};
use crate::cli::args::{TvCommand, TvAiCommand, GlobalArgs};
use crate::cli::output::OutputWriter;
use crate::error::{ExitStatus, Result};
use std::time::Duration;

/// Handle TV commands
pub async fn handle_tv(cmd: TvCommand, global: &GlobalArgs) -> Result<ExitStatus> {
    let client = GdeltClient::with_timeout(Duration::from_secs(global.timeout))?;
    let api = TvApi::new(client);

    match cmd {
        TvCommand::Search(args) => {
            let params = TvSearchParams {
                query: args.query,
                timespan: Some(args.timespan),
                station: args.station,
                max_records: Some(args.max_records),
            };

            let response = api.search(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }

        TvCommand::Clips(args) => {
            let params = TvClipsParams {
                query: args.query,
                timespan: Some(args.timespan),
            };

            let response = api.clips(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }

        TvCommand::Timeline(args) => {
            let params = TvTimelineParams {
                query: args.query,
                timespan: Some(args.timespan),
                by_station: args.by_station,
            };

            let response = api.timeline(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }

        TvCommand::Stations(args) => {
            let params = TvStationsParams {
                country: args.country,
            };

            let response = api.stations(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }
    }
}

/// Handle TV AI commands
pub async fn handle_tvai(cmd: TvAiCommand, global: &GlobalArgs) -> Result<ExitStatus> {
    let client = GdeltClient::with_timeout(Duration::from_secs(global.timeout))?;
    let api = TvAiApi::new(client);

    match cmd {
        TvAiCommand::Search(args) => {
            let params = TvAiSearchParams {
                query: args.query,
                timespan: Some(args.timespan),
                captions: args.captions,
                ocr: args.ocr,
                asr: args.asr,
            };

            let response = api.search(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }

        TvAiCommand::Concepts(args) => {
            let params = TvAiConceptsParams {
                concept: args.concept,
                timespan: Some(args.timespan),
            };

            let response = api.concepts(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }

        TvAiCommand::Visual(args) => {
            let params = TvAiVisualParams {
                entity: args.entity,
                timespan: Some(args.timespan),
            };

            let response = api.visual(params).await?;
            let output = OutputWriter::new(global);
            output.write_value(&response)?;

            Ok(ExitStatus::Success)
        }
    }
}
