use anyhow::Result;
use async_trait::async_trait;

use super::panel_adapter::{DiscoveredServer, FileEntry, PanelAdapter, ServerStatus};

/// Pterodactyl panel adapter.
///
/// Communicates with Pterodactyl (or Pelican) panels via their REST API.
/// Uses Application API keys for server management and Client API keys
/// for console/file operations. Implements the unified PanelAdapter trait.
pub struct PterodactylAdapter {
    pub api_endpoint: String,
    pub api_key: String,
}

impl PterodactylAdapter {
    pub fn new(api_endpoint: String, api_key: String) -> Self {
        Self {
            api_endpoint,
            api_key,
        }
    }
}

#[async_trait]
impl PanelAdapter for PterodactylAdapter {
    async fn test_connection(&self) -> Result<bool> {
        // TODO: GET /api/application/servers to verify API key validity
        todo!()
    }

    async fn discover_servers(&self) -> Result<Vec<DiscoveredServer>> {
        // TODO: Paginate /api/application/servers, filter for Rust egg, map to DiscoveredServer
        todo!()
    }

    async fn get_server_status(&self, _server_id: &str) -> Result<ServerStatus> {
        // TODO: GET /api/client/servers/{id}/resources for live stats
        todo!()
    }

    async fn start_server(&self, _server_id: &str) -> Result<()> {
        // TODO: POST /api/client/servers/{id}/power with signal=start
        todo!()
    }

    async fn stop_server(&self, _server_id: &str) -> Result<()> {
        // TODO: POST /api/client/servers/{id}/power with signal=stop
        todo!()
    }

    async fn restart_server(&self, _server_id: &str) -> Result<()> {
        // TODO: POST /api/client/servers/{id}/power with signal=restart
        todo!()
    }

    async fn send_command(&self, _server_id: &str, _command: &str) -> Result<String> {
        // TODO: POST /api/client/servers/{id}/command
        todo!()
    }

    async fn get_file(&self, _server_id: &str, _path: &str) -> Result<Vec<u8>> {
        // TODO: GET /api/client/servers/{id}/files/contents?file={path}
        todo!()
    }

    async fn put_file(&self, _server_id: &str, _path: &str, _data: &[u8]) -> Result<()> {
        // TODO: POST /api/client/servers/{id}/files/write?file={path}
        todo!()
    }

    async fn delete_file(&self, _server_id: &str, _path: &str) -> Result<()> {
        // TODO: POST /api/client/servers/{id}/files/delete with body
        todo!()
    }

    async fn list_files(&self, _server_id: &str, _path: &str) -> Result<Vec<FileEntry>> {
        // TODO: GET /api/client/servers/{id}/files/list?directory={path}
        todo!()
    }

    async fn trigger_steam_update(&self, _server_id: &str) -> Result<()> {
        // TODO: Pterodactyl doesn't have native SteamCMD trigger — send console command
        // or use startup command reinstall via /api/client/servers/{id}/settings/reinstall
        todo!()
    }
}
