// Copyright (c) 2023-2024 Retake, Inc.
//
// This file is part of ParadeDB - Postgres for Search and Analytics
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use pgrx::*;

#[derive(PartialEq)]
pub enum FdwHandler {
    Csv,
    Json,
    Parquet,
    Delta,
    Iceberg,
    Spatial,
    Other,
}

/// These names are auto-generated by supabase-wrappers
/// If the FDW is called MyContainerFdw, the handler name will be my_container_fdw_handler
impl From<&str> for FdwHandler {
    fn from(handler_name: &str) -> Self {
        match handler_name {
            "csv_fdw_handler" => FdwHandler::Csv,
            "json_fdw_handler" => FdwHandler::Json,
            "parquet_fdw_handler" => FdwHandler::Parquet,
            "delta_fdw_handler" => FdwHandler::Delta,
            "iceberg_fdw_handler" => FdwHandler::Iceberg,
            "spatial_fdw_handler" => FdwHandler::Spatial,
            _ => FdwHandler::Other,
        }
    }
}

impl From<*mut pg_sys::ForeignServer> for FdwHandler {
    fn from(server: *mut pg_sys::ForeignServer) -> Self {
        let oid = unsafe { (*server).fdwid };
        let fdw = unsafe { pg_sys::GetForeignDataWrapper(oid) };
        let handler_oid = unsafe { (*fdw).fdwhandler };
        let proc_tuple = unsafe {
            pg_sys::SearchSysCache1(
                pg_sys::SysCacheIdentifier::PROCOID as i32,
                handler_oid.into_datum().unwrap(),
            )
        };
        let pg_proc = unsafe { pg_sys::GETSTRUCT(proc_tuple) as pg_sys::Form_pg_proc };
        let handler_name = unsafe { name_data_to_str(&(*pg_proc).proname) };
        unsafe { pg_sys::ReleaseSysCache(proc_tuple) };

        FdwHandler::from(handler_name)
    }
}

impl From<*mut pg_sys::ForeignTable> for FdwHandler {
    fn from(table: *mut pg_sys::ForeignTable) -> Self {
        let server = unsafe { pg_sys::GetForeignServer((*table).serverid) };
        FdwHandler::from(server)
    }
}
