<?php
/**
 * Plugin Name: Carei Reservation
 * Description: Formularz rezerwacji samochodu zintegrowany z Softra Rent API, jako widget Elementor.
 * Version: 1.0.0
 * Author: Carei
 * Text Domain: carei-reservation
 * Requires PHP: 7.4
 */

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

define( 'CAREI_RESERVATION_VERSION', '1.0.0' );
define( 'CAREI_RESERVATION_PATH', plugin_dir_path( __FILE__ ) );
define( 'CAREI_RESERVATION_URL', plugin_dir_url( __FILE__ ) );

/**
 * Parse .env file (format: "key: value")
 */
function carei_parse_env() {
    $env_path = ABSPATH . '.env';
    if ( ! file_exists( $env_path ) ) {
        return array();
    }

    $lines = file( $env_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
    $env   = array();

    foreach ( $lines as $line ) {
        $line = trim( $line );
        if ( '' === $line || '#' === $line[0] ) {
            continue;
        }
        $pos = strpos( $line, ':' );
        if ( false === $pos ) {
            continue;
        }
        $key         = trim( substr( $line, 0, $pos ) );
        $value       = trim( substr( $line, $pos + 1 ) );
        $env[ $key ] = $value;
    }

    return $env;
}

/**
 * Load includes
 */
require_once CAREI_RESERVATION_PATH . 'includes/class-softra-api.php';
require_once CAREI_RESERVATION_PATH . 'includes/class-rest-proxy.php';
require_once CAREI_RESERVATION_PATH . 'includes/class-admin-panel.php';

/**
 * Initialize plugin on plugins_loaded
 */
add_action( 'plugins_loaded', function () {
    $env = carei_parse_env();

    $api_url  = isset( $env['url'] ) ? $env['url'] : '';
    $username = isset( $env['username'] ) ? $env['username'] : '';
    $password = isset( $env['password'] ) ? $env['password'] : '';

    if ( empty( $api_url ) || empty( $username ) || empty( $password ) ) {
        add_action( 'admin_notices', function () {
            echo '<div class="notice notice-error"><p><strong>Carei Reservation:</strong> Brak konfiguracji API w pliku .env (url, username, password).</p></div>';
        } );
        return;
    }

    // Initialize Softra API singleton
    Carei_Softra_API::init( $api_url, $username, $password );

    // Initialize REST proxy
    new Carei_REST_Proxy();

    // Initialize admin panel
    new Carei_Admin_Panel();
} );

/**
 * Register Elementor widget
 */
add_action( 'elementor/widgets/register', function ( $widgets_manager ) {
    require_once CAREI_RESERVATION_PATH . 'includes/class-elementor-widget.php';
    $widgets_manager->register( new Carei_Reservation_Widget() );
} );

/**
 * Enqueue frontend assets
 */
add_action( 'wp_enqueue_scripts', function () {
    wp_register_style(
        'carei-reservation-css',
        CAREI_RESERVATION_URL . 'assets/css/carei-reservation.css',
        array(),
        CAREI_RESERVATION_VERSION
    );
    wp_register_script(
        'carei-reservation-js',
        CAREI_RESERVATION_URL . 'assets/js/carei-reservation.js',
        array(),
        CAREI_RESERVATION_VERSION,
        true
    );
    wp_localize_script( 'carei-reservation-js', 'careiReservation', array(
        'restUrl' => esc_url_raw( rest_url( 'carei/v1/' ) ),
        'nonce'   => wp_create_nonce( 'wp_rest' ),
    ) );
} );
