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

class TMNL_Adapter_Manual extends TMNL_Source_Adapter {

    public function get_id() {
        return 'manual';
    }

    public function get_label() {
        return __( 'Testimonials', 'elplan-kuchikomi-wall' );
    }

    public function get_icon() {
        return 'dashicons-edit';
    }

    public function fetch( $force = false ) {
        return $this->get_cached();
    }

    private function text_source_types() {
        return array( 'manual', 'x_collected', 'ig_collected', 'fb_collected' );
    }

    public function get_cached() {
        return TMNL_DB::get_testimonials( array(
            'visible_only' => false,
            'source_type'  => $this->text_source_types(),
        ));
    }

    public function clear_cache() {
        // Manual entries are in DB, nothing to clear
    }

    public function get_status() {
        $src   = self::get_source_settings( 'manual' );
        $items = TMNL_DB::get_testimonials( array( 'visible_only' => false, 'source_type' => 'manual' ) );
        return array(
            'is_enabled' => true, // Manual is always enabled
            'count'      => count( $items ),
            'updated_at' => $src['updated_at'],
            'error'      => null,
        );
    }

    public function render_settings() {
        echo '<p>' . esc_html__( 'Manage testimonials from the Testimonials menu.', 'elplan-kuchikomi-wall' ) . '</p>';
    }

    public function save_settings( $post_data ) {
        // No settings to save for manual
    }

    public function get_normalized_reviews( $min_rating = 1 ) {
        $items = TMNL_DB::get_testimonials( array(
            'visible_only' => true,
            'min_rating'   => $min_rating,
            'source_type'  => $this->text_source_types(),
        ));
        $normalized = array();
        foreach ( $items as $item ) {
            $normalized[] = array(
                'author_name'  => $item->author_name,
                'rating'       => $item->rating,
                'text'         => $item->text,
                'date'         => $item->review_date,
                'source_type'  => 'manual',
                'source_label' => $item->source_label ?: __( 'Manual', 'elplan-kuchikomi-wall' ),
                'external_id'  => null,
                'author_icon'  => null,
                'embed_html'   => null,
                'url'          => null,
                'sort_order'   => $item->sort_order,
            );
        }
        return $normalized;
    }
}
