<?php
/**
 * MTC — Trail camera profiles
 *
 * Preset parameters for common trail camera models available on the market.
 * A profile serves as a template when adding a camera — it auto-fills
 * the form, but the user can adjust the values.
 *
 * Profiles are split into per-manufacturer files in includes/profiles/.
 *
 * @package My Trail Camera
 * @since 8.2.0
 */

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

/**
 * Returns list of all available trail camera profiles.
 *
 * Profile structure:
 *   label            - Model name
 *   manufacturer     - Manufacturer
 *   category         - Category: basic | 4g | solar | premium
 *   fov              - Field of view angle in degrees
 *   battery_capacity - Battery capacity in mAh
 *   battery_type     - Battery type: 8xAA, 12xAA, builtin, etc.
 *   power_types      - Supported power types (array)
 *   has_sim          - Has SIM slot (4G/LTE)
 *   has_cloud        - Sends photos to cloud / email
 *   has_wifi         - Has WiFi
 *   has_solar        - Has built-in/included solar panel
 *   video_resolutions - Supported video resolutions (array)
 *   drain_photo      - Consumption per photo (mAh)
 *   drain_video_sec  - Consumption per 1 second of video (mAh)
 *   drain_standby    - Standby consumption (mAh/day)
 *   drain_4g         - Extra 4G module consumption (mAh/day), 0 if none
 *   default_accessories - Default accessories
 *   tip              - Tip for the user
 */
function mtcam_get_camera_profiles() {
    $profiles = array();
    $profiles_dir = __DIR__ . '/profiles';

    // Load all manufacturer profile files
    foreach ( glob( $profiles_dir . '/*.php' ) as $file ) {
        $manufacturer_profiles = include $file;
        if ( is_array( $manufacturer_profiles ) ) {
            $profiles = array_merge( $profiles, $manufacturer_profiles );
        }
    }

    // Custom model — always last, always available
    $profiles['custom'] = array(
        'label'            => __('— Custom camera —', 'my-trail-camera'),
        'manufacturer'     => '',
        'category'         => 'basic',
        'fov'              => 90,
        'battery_capacity' => 13000,
        'battery_type'     => '8× AA',
        'power_types'      => array('lithium', 'solar_lithium', 'solar_lead', 'external'),
        'has_sim'          => false,
        'has_cloud'        => false,
        'has_wifi'         => false,
        'has_solar'        => false,
        'video_resolutions' => array('4K', '2K', '1296p', '1080p', '720p', '480p'),
        'drain_photo'      => 2.0,
        'drain_video_sec'  => 7.0,
        'drain_standby'    => 10,
        'drain_4g'         => 0,
        'default_accessories' => array(),
        'tip' => __('Enter parameters manually according to your camera specifications.', 'my-trail-camera'),
    );

    // Merge custom profiles from DB
    $custom = get_option('mtcam_custom_profiles', array());
    if (!empty($custom) && is_array($custom)) {
        foreach ($custom as $id => $profile) {
            $profile['category'] = 'user';
            $profiles[$id] = $profile;
        }
    }

    return $profiles;
}

/**
 * Get profile by ID, or 'custom' if not found.
 */
function mtcam_get_camera_profile($profile_id) {
    $profiles = mtcam_get_camera_profiles();
    return $profiles[$profile_id] ?? $profiles['custom'];
}

/**
 * Get profiles grouped by category (for <optgroup> in select).
 */
function mtcam_get_profiles_grouped() {
    $profiles = mtcam_get_camera_profiles();
    $categories = array(
        'user'    => '👤 ' . __('Custom models', 'my-trail-camera'),
        'basic'   => '📷 ' . __('Basic (no SIM)', 'my-trail-camera'),
        '4g'      => '📶 4G / LTE',
        'solar'   => '☀️ ' . __('Solar', 'my-trail-camera'),
        'premium' => '⭐ ' . __('Premium', 'my-trail-camera'),
    );

    $grouped = array();
    foreach ($profiles as $id => $profile) {
        if ($id === 'custom') continue;
        $cat = $profile['category'] ?? 'basic';
        $grouped[$cat][$id] = $profile;
    }

    // Remove empty categories
    foreach (array_keys($categories) as $cat) {
        if (empty($grouped[$cat])) unset($categories[$cat]);
    }

    return array('categories' => $categories, 'groups' => $grouped);
}

