<?php
/**
 * MTC — Trail camera management (actions)
 *
 * Trait for the main plugin class.
 * Pages + shortcodes.
 *
 * Form processing moved to class-camera-admin-handlers.php
 * Bait generation moved to class-bait-service.php
 * Import/export moved to class-import-export-service.php
 *
 * @package My Trail Camera
 * @since 8.1.0
 */

if (!defined('ABSPATH')) exit;

trait Mtcam_Cameras_Actions {

    public function add_camera_page() {
        $tracker = $this;
        include plugin_dir_path($this->plugin_file) . 'admin/pages/page-add-camera.php';
    }

    public function dashboard_page() {
        $tracker = $this;
        include plugin_dir_path($this->plugin_file) . 'admin/pages/page-dashboard.php';
    }

    public function locations_page() {
        include plugin_dir_path($this->plugin_file) . 'admin/pages/page-locations.php';
    }

    public function settings_page() {

        // Redirect-causing handlers (import, profiles, settings save) moved to
        // Mtcam_Camera_Admin_Handlers::process_settings_actions() — runs on admin_init

        $settings = array(
            'notifications' => $this->get_cached_option('mtcam_notifications', 'yes'),
            'notify_email' => $this->get_cached_option('mtcam_notify_email', $this->get_cached_option('admin_email')),
            'custom_text' => $this->get_cached_option('mtcam_email_custom_text', __("MTC security system detected!", "my-trail-camera")),
            'show_ip' => $this->get_cached_option('mtcam_show_ip', 'yes'),
            'show_photo' => $this->get_cached_option('mtcam_show_photo', 'yes'),

        );

        // Public widget settings (stolen + active) — loaded via loop
        $widget_fields = array( 'name', 'manufacturer', 'imei', 'location', 'gps', 'dates', 'baits', 'security', 'gps_trackers', 'photo' );
        $stolen_defaults = array_fill_keys( $widget_fields, 'yes' );
        $active_defaults = array( 'name' => 'yes', 'manufacturer' => 'no', 'imei' => 'no', 'location' => 'no', 'gps' => 'no', 'dates' => 'yes', 'baits' => 'no', 'security' => 'no', 'gps_trackers' => 'no', 'photo' => 'yes' );
        foreach ( array( 'stolen' => $stolen_defaults, 'active' => $active_defaults ) as $type => $defaults ) {
            foreach ( $defaults as $field => $default ) {
                $settings["public_{$type}_show_{$field}"] = $this->get_cached_option( "mtcam_public_{$type}_show_{$field}", $default );
            }
        }
        $settings['public_stolen_show_police'] = $this->get_cached_option( 'mtcam_public_stolen_show_police', 'yes' );

        $settings += array(
            'show_battery_stolen' => $this->get_cached_option('mtcam_show_battery_stolen', 'yes'),
            'ip_source' => $this->get_cached_option('mtcam_ip_source', 'remote_addr'),
        );
        $cameras = $this->get_cameras();

        // Prepare system info for template (no DB queries in template)
        global $wpdb;
        $total_records = wp_cache_get( 'mtcam_total_records' );
        if ( false === $total_records ) {
            $total_records = intval( mtcam_db_get_var( 'SELECT COUNT(*) FROM `' . $wpdb->prefix . 'mtcam_detections`' ) );
            wp_cache_set( 'mtcam_total_records', $total_records );
        }
        $system_info = array(
            'plugin_version' => mtcam_get_version_display(),
            'wp_version' => get_bloginfo('version'),
            'php_version' => PHP_VERSION,
            'total_records' => $total_records,
        );

        include plugin_dir_path($this->plugin_file) . 'admin/pages/page-settings.php';
    }

    // ============================================================================
    // UNIFIED SHORTCODE (DRY principle)
    // ============================================================================

    /**
     * Universal function for stolen/active camera widgets
     */
    private function render_camera_widget($widget_type = 'stolen', $atts = []) {
        $atts = shortcode_atts([
            'show_all' => 'no',
            'limit' => '0'
        ], $atts);

        $cameras = $this->get_cameras();

        // Batch load detection counts (only once!)
        $detection_counts = $this->get_detection_counts();

        $prefix = 'mtcam_public_' . $widget_type . '_show_';

        // Defaults by type
        $defaults = [
            'stolen' => [
                'name' => 'yes', 'manufacturer' => 'yes', 'imei' => 'yes',
                'location' => 'yes', 'gps' => 'yes', 'dates' => 'yes',
                'baits' => 'yes', 'security' => 'yes', 'gps_trackers' => 'yes',
                'photo' => 'yes', 'police' => 'yes'
            ],
            'active' => [
                'name' => 'yes', 'manufacturer' => 'no', 'imei' => 'no',
                'location' => 'no', 'gps' => 'no', 'dates' => 'yes',
                'baits' => 'no', 'security' => 'no', 'gps_trackers' => 'no',
                'photo' => 'yes', 'police' => 'no'
            ]
        ];

        // Dynamic loading of settings
        $show = [];
        foreach ($defaults[$widget_type] as $field => $default) {
            $show[$field] = $this->get_cached_option($prefix . $field, $default);
        }

        ob_start();
        include plugin_dir_path($this->plugin_file) . 'admin/widgets/widget-stolen-cameras.php';
        return ob_get_clean();
    }

    public function stolen_cameras_shortcode($atts) {
        return $this->render_camera_widget('stolen', $atts);
    }

    public function active_cameras_shortcode($atts) {
        return $this->render_camera_widget('active', $atts);
    }

}
