<?php
if ( ! defined( 'ABSPATH' ) ) exit;

abstract class TMNL_Source_Adapter {

    abstract public function get_id();
    abstract public function get_label();
    abstract public function get_icon();

    /**
     * Fetch reviews from external source (API call etc.)
     * @param bool $force Force refresh ignoring cache
     * @return array|WP_Error
     */
    abstract public function fetch( $force = false );

    /**
     * Get cached/stored reviews without external API call
     * @return array
     */
    abstract public function get_cached();

    /**
     * Clear cached data
     */
    abstract public function clear_cache();

    /**
     * Get source status info
     * @return array { is_enabled, count, updated_at, error }
     */
    public function get_status() {
        $settings = self::get_source_settings( $this->get_id() );
        $cached = $this->get_cached();
        return array(
            'is_enabled'  => ! empty( $settings['is_enabled'] ),
            'count'       => count( $cached ),
            'updated_at'  => $settings['updated_at'] ?? null,
            'error'       => null,
        );
    }

    /**
     * Render settings form HTML for admin page
     */
    abstract public function render_settings();

    /**
     * Save settings from POST data
     * @param array $post_data $_POST data
     */
    abstract public function save_settings( $post_data );

    /**
     * Get normalized reviews in unified format
     * @param int $min_rating Minimum rating filter
     * @return array of normalized review arrays
     */
    abstract public function get_normalized_reviews( $min_rating = 1 );

    // =============================================
    // Shared helpers for source settings table
    // =============================================

    /**
     * Get source settings from tmnl_source_settings table
     */
    public static function get_source_settings( $source_type ) {
        global $wpdb;
        $table = $wpdb->prefix . 'tmnl_source_settings';
        $row = $wpdb->get_row(
            $wpdb->prepare( "SELECT * FROM {$table} WHERE source_type = %s", $source_type )
        );
        if ( ! $row ) {
            return array( 'is_enabled' => false, 'settings' => array(), 'updated_at' => null );
        }
        return array(
            'is_enabled' => (bool) $row->is_enabled,
            'settings'   => json_decode( $row->settings, true ) ?: array(),
            'updated_at' => $row->updated_at,
        );
    }

    /**
     * Save source settings to tmnl_source_settings table
     */
    public static function save_source_settings( $source_type, $is_enabled, $settings = array() ) {
        global $wpdb;
        $table = $wpdb->prefix . 'tmnl_source_settings';
        $existing = $wpdb->get_var(
            $wpdb->prepare( "SELECT id FROM {$table} WHERE source_type = %s", $source_type )
        );

        $data = array(
            'source_type' => $source_type,
            'is_enabled'  => $is_enabled ? 1 : 0,
            'settings'    => wp_json_encode( $settings ),
            'updated_at'  => current_time( 'mysql' ),
        );

        if ( $existing ) {
            unset( $data['source_type'] );
            $wpdb->update( $table, $data, array( 'source_type' => $source_type ) );
        } else {
            $wpdb->insert( $table, $data );
        }
    }

    /**
     * Toggle source enabled/disabled
     */
    public static function toggle_source( $source_type ) {
        $current = self::get_source_settings( $source_type );
        self::save_source_settings( $source_type, ! $current['is_enabled'], $current['settings'] );
        return ! $current['is_enabled'];
    }
}
