<?php
/**
 * MTC - Battery presets catalog
 * Catalog of concrete battery products used in trail cameras, plus chemistry
 * and self-discharge rates used by the battery estimation.
 *
 * @package My Trail Camera
 * @since 8.2.4
 */

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

/**
 * Full battery preset catalog.
 *
 * chemistry:
 *   - 'primary_lifes2' — non-rechargeable Lithium Iron Disulfide (AA/AAA).
 *                       Excellent cold performance, ~1 %/year self-discharge.
 *   - 'li_ion'        — rechargeable Li-ion / LiPo (18650, 21700, internal packs).
 *                       Poor cold performance, ~2 %/month self-discharge.
 *
 * format: 'AA' | 'AAA' | '18650' | '21700' | 'internal'
 * mah:     nominal capacity per cell
 * voltage: nominal voltage per cell
 *
 * @return array
 */
function mtcam_get_battery_presets() {
    return array(
        // ── Primary LiFeS₂ AA ──
        'energizer_l91' => array(
            'brand'     => 'Energizer',
            'model'     => 'Ultimate Lithium L91',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AA',
            'mah'       => 3000,
            'voltage'   => 1.5,
        ),
        'varta_6106' => array(
            'brand'     => 'Varta',
            'model'     => 'Professional Lithium 6106',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AA',
            'mah'       => 2900,
            'voltage'   => 1.5,
        ),
        'gp_extra_lithium_aa' => array(
            'brand'     => 'GP',
            'model'     => 'Extra Lithium AA',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AA',
            'mah'       => 2900,
            'voltage'   => 1.5,
        ),
        'fujitsu_premium_lithium_aa' => array(
            'brand'     => 'Fujitsu',
            'model'     => 'Premium Lithium AA',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AA',
            'mah'       => 3000,
            'voltage'   => 1.5,
        ),
        'panasonic_industrial_lithium_aa' => array(
            'brand'     => 'Panasonic',
            'model'     => 'Industrial Lithium AA',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AA',
            'mah'       => 2900,
            'voltage'   => 1.5,
        ),
        'generic_primary_lithium_aa' => array(
            'brand'     => 'Generic',
            'model'     => 'Lithium AA (no-name)',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AA',
            'mah'       => 2700,
            'voltage'   => 1.5,
        ),

        // ── Primary LiFeS₂ AAA ──
        'energizer_l92' => array(
            'brand'     => 'Energizer',
            'model'     => 'Ultimate Lithium L92',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AAA',
            'mah'       => 1200,
            'voltage'   => 1.5,
        ),
        'varta_6103' => array(
            'brand'     => 'Varta',
            'model'     => 'Professional Lithium 6103',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AAA',
            'mah'       => 1100,
            'voltage'   => 1.5,
        ),
        'gp_extra_lithium_aaa' => array(
            'brand'     => 'GP',
            'model'     => 'Extra Lithium AAA',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AAA',
            'mah'       => 1100,
            'voltage'   => 1.5,
        ),
        'generic_primary_lithium_aaa' => array(
            'brand'     => 'Generic',
            'model'     => 'Lithium AAA (no-name)',
            'chemistry' => 'primary_lifes2',
            'format'    => 'AAA',
            'mah'       => 1000,
            'voltage'   => 1.5,
        ),

        // ── Li-ion 18650 ──
        'panasonic_ncr18650b' => array(
            'brand'     => 'Panasonic',
            'model'     => 'NCR18650B',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3400,
            'voltage'   => 3.7,
        ),
        'panasonic_ncr18650ga' => array(
            'brand'     => 'Panasonic',
            'model'     => 'NCR18650GA',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3350,
            'voltage'   => 3.6,
        ),
        'samsung_35e' => array(
            'brand'     => 'Samsung',
            'model'     => 'INR18650-35E',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3500,
            'voltage'   => 3.6,
        ),
        'samsung_30q' => array(
            'brand'     => 'Samsung',
            'model'     => 'INR18650-30Q',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3000,
            'voltage'   => 3.6,
        ),
        'lg_mj1' => array(
            'brand'     => 'LG',
            'model'     => 'INR18650-MJ1',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3500,
            'voltage'   => 3.635,
        ),
        'lg_hg2' => array(
            'brand'     => 'LG',
            'model'     => 'INR18650-HG2',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3000,
            'voltage'   => 3.6,
        ),
        'molicel_m35a' => array(
            'brand'     => 'Molicel',
            'model'     => 'INR18650-M35A',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3500,
            'voltage'   => 3.6,
        ),
        'sony_vtc6' => array(
            'brand'     => 'Sony / Murata',
            'model'     => 'VTC6',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 3000,
            'voltage'   => 3.6,
        ),
        'generic_18650' => array(
            'brand'     => 'Generic',
            'model'     => '18650 Li-ion (no-name)',
            'chemistry' => 'li_ion',
            'format'    => '18650',
            'mah'       => 2200,
            'voltage'   => 3.7,
        ),

        // ── Li-ion 21700 ──
        'samsung_50e' => array(
            'brand'     => 'Samsung',
            'model'     => 'INR21700-50E',
            'chemistry' => 'li_ion',
            'format'    => '21700',
            'mah'       => 5000,
            'voltage'   => 3.6,
        ),
        'samsung_40t' => array(
            'brand'     => 'Samsung',
            'model'     => 'INR21700-40T',
            'chemistry' => 'li_ion',
            'format'    => '21700',
            'mah'       => 4000,
            'voltage'   => 3.6,
        ),
        'molicel_p42a' => array(
            'brand'     => 'Molicel',
            'model'     => 'INR21700-P42A',
            'chemistry' => 'li_ion',
            'format'    => '21700',
            'mah'       => 4200,
            'voltage'   => 3.7,
        ),

        // ── Internal LiPo / Li-ion packs ──
        'internal_lipo_1s' => array(
            'brand'     => 'Built-in',
            'model'     => 'LiPo pack 1S (3.7 V)',
            'chemistry' => 'li_ion',
            'format'    => 'internal',
            'mah'       => 3000,
            'voltage'   => 3.7,
        ),
        'internal_lipo_2s' => array(
            'brand'     => 'Built-in',
            'model'     => 'LiPo pack 2S (7.4 V)',
            'chemistry' => 'li_ion',
            'format'    => 'internal',
            'mah'       => 5000,
            'voltage'   => 7.4,
        ),
        'internal_liion_3s' => array(
            'brand'     => 'Built-in',
            'model'     => 'Li-ion pack 3S (11.1 V)',
            'chemistry' => 'li_ion',
            'format'    => 'internal',
            'mah'       => 5000,
            'voltage'   => 11.1,
        ),
    );
}

/**
 * Format order for optgroup grouping in dropdown.
 *
 * @return array
 */
function mtcam_battery_format_labels() {
    return array(
        'AA'       => __( 'AA (1.5 V lithium primary)', 'my-trail-camera' ),
        'AAA'      => __( 'AAA (1.5 V lithium primary)', 'my-trail-camera' ),
        '18650'    => __( '18650 (Li-ion rechargeable)', 'my-trail-camera' ),
        '21700'    => __( '21700 (Li-ion rechargeable)', 'my-trail-camera' ),
        'internal' => __( 'Built-in battery packs', 'my-trail-camera' ),
    );
}

/**
 * Get one preset by id, or null.
 *
 * @param string $id Preset id.
 * @return array|null
 */
function mtcam_get_battery_preset( $id ) {
    if ( empty( $id ) ) return null;
    $all = mtcam_get_battery_presets();
    return $all[ $id ] ?? null;
}

/**
 * Human label for preset.
 *
 * @param string $id Preset id.
 * @return string
 */
function mtcam_battery_preset_label( $id ) {
    $p = mtcam_get_battery_preset( $id );
    if ( $p === null ) return '';
    return sprintf( '%s %s (%d mAh @ %.1f V)', $p['brand'], $p['model'], $p['mah'], $p['voltage'] );
}

/**
 * Presets grouped by format, in the order defined by mtcam_battery_format_labels().
 *
 * @return array Map format => array of [ id => label ].
 */
function mtcam_get_battery_presets_grouped() {
    $grouped = array();
    foreach ( array_keys( mtcam_battery_format_labels() ) as $fmt ) {
        $grouped[ $fmt ] = array();
    }
    foreach ( mtcam_get_battery_presets() as $id => $p ) {
        $fmt = $p['format'];
        if ( ! isset( $grouped[ $fmt ] ) ) {
            $grouped[ $fmt ] = array();
        }
        $grouped[ $fmt ][ $id ] = sprintf( '%s %s — %d mAh', $p['brand'], $p['model'], $p['mah'] );
    }
    return $grouped;
}

/**
 * Resolve effective battery capacity (mAh) and voltage for a trail camera.
 *
 * Priority:
 *  1. If $cam has a valid battery_preset and cell_count > 0 → preset.mah × cell_count.
 *  2. Otherwise fall back to profile['battery_capacity'] or MTCAM_BATTERY_CAPACITY.
 *
 * Chemistry defaults to 'primary_lifes2' when no preset is chosen (AA lithium is the
 * most common setup — matches the migration from legacy 'lithium' power type).
 *
 * @param array $cam     Camera row.
 * @param array $profile Profile array (may be empty).
 * @return array { 'capacity_mah' => float, 'voltage' => float, 'chemistry' => string, 'preset_id' => string }
 */
function mtcam_resolve_battery_spec( $cam, $profile = array() ) {
    $preset_id  = $cam['battery_preset'] ?? '';
    $cell_count = intval( $cam['cell_count'] ?? 0 );
    $preset     = mtcam_get_battery_preset( $preset_id );

    if ( $preset !== null && $cell_count > 0 ) {
        return array(
            'capacity_mah' => (float) ( $preset['mah'] * $cell_count ),
            'voltage'      => (float) $preset['voltage'],
            'chemistry'    => $preset['chemistry'],
            'preset_id'    => $preset_id,
        );
    }

    $default_cap = defined( 'MTCAM_BATTERY_CAPACITY' ) ? MTCAM_BATTERY_CAPACITY : 4400;
    $default_v   = defined( 'MTCAM_BATTERY_VOLTAGE' )  ? MTCAM_BATTERY_VOLTAGE  : 3.7;

    return array(
        'capacity_mah' => (float) ( $profile['battery_capacity'] ?? $default_cap ),
        'voltage'      => $default_v,
        'chemistry'    => 'primary_lifes2',
        'preset_id'    => '',
    );
}

/**
 * Self-discharge as a daily fraction of capacity.
 *
 *   primary_lifes2 → 1 %/year   ≈ 0.0000274/day
 *   li_ion         → 2 %/month  ≈ 0.000657/day
 *
 * @param string $chemistry Chemistry id.
 * @return float Daily fraction (0-1).
 */
function mtcam_self_discharge_daily_rate( $chemistry ) {
    if ( $chemistry === 'li_ion' ) {
        return 0.02 / 30.0;
    }
    // primary_lifes2 and unknown → negligible.
    return 0.01 / 365.0;
}
