<?php
/**
 * Plugin Name: OVP-Sponsorizare
 * Plugin URI:  https://wordpress.org/plugins/ovp-sponsorizare/
 * Description: Gestionare online Formular 230 (persoane fizice), Formular 177 (persoane juridice) si contracte de sponsorizare. Conform legislatiei in vigoare.
 * Version:     2.1.5
 * Author:      Ovidiu Pantiru - OVP-Digital
 * Author URI:  https://ovidiupantiru.ro
 * License:     GPL-2.0+
 * Text Domain: ovp-sponsorizare
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 */

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

// Constante principale - prefix OVPSPO (>4 caractere)
if ( ! defined( 'OVPSPO_VERSION' ) )    define( 'OVPSPO_VERSION',    '2.1.5' );
if ( ! defined( 'OVPSPO_FILE' ) )       define( 'OVPSPO_FILE',       __FILE__ );
if ( ! defined( 'OVPSPO_PATH' ) )       define( 'OVPSPO_PATH',       plugin_dir_path( __FILE__ ) );
if ( ! defined( 'OVPSPO_URL' ) )        define( 'OVPSPO_URL',        plugin_dir_url( __FILE__ ) );
if ( ! defined( 'OVPSPO_TCPDF_PATH' ) ) define( 'OVPSPO_TCPDF_PATH', OVPSPO_PATH . 'lib/tcpdf/tcpdf.php' );

// Formular 230 (persoane fizice)
require_once OVPSPO_PATH . 'includes/class-settings.php';
require_once OVPSPO_PATH . 'includes/class-form.php';
require_once OVPSPO_PATH . 'includes/class-pdf-fallback.php';
require_once OVPSPO_PATH . 'includes/class-pdf.php';
require_once OVPSPO_PATH . 'includes/class-pdf-anaf.php';
require_once OVPSPO_PATH . 'includes/class-email.php';
require_once OVPSPO_PATH . 'includes/class-ajax.php';

// Formular 177 (persoane juridice)
require_once OVPSPO_PATH . 'includes/class-form177.php';
require_once OVPSPO_PATH . 'includes/class-pdf177.php';
require_once OVPSPO_PATH . 'includes/class-email177.php';
require_once OVPSPO_PATH . 'includes/class-ajax177.php';

// Contract de Sponsorizare
require_once OVPSPO_PATH . 'includes/class-form-contract.php';
require_once OVPSPO_PATH . 'includes/class-pdf-contract.php';
require_once OVPSPO_PATH . 'includes/class-email-contract.php';
require_once OVPSPO_PATH . 'includes/class-ajax-contract.php';

add_action( 'plugins_loaded', function() {
    OVPSPO_Settings::init();
    OVPSPO_Form::init();
    OVPSPO_Ajax::init();
    OVPSPO_Form177::init();
    OVPSPO_Ajax177::init();
    OVPSPO_Form_Contract::init();
    OVPSPO_Ajax_Contract::init();
} );

// Verifica si creeaza tabelele la fiecare request (inclusiv AJAX frontend)
add_action( 'init', 'ovpspo_ensure_tables' );

/**
 * Asigura existenta tabelelor bazei de date.
 */
function ovpspo_ensure_tables() {
    if ( defined( 'OVPSPO_TABLES_CHECKED' ) ) return;
    define( 'OVPSPO_TABLES_CHECKED', true );

    global $wpdb;
    $t230 = $wpdb->prefix . 'f230_submissions';
    $t177 = $wpdb->prefix . 'f177_submissions';
    $tc   = $wpdb->prefix . 'contracte_sponsorizare';

    $missing = false;
    if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $t230 ) ) !== $t230 ) $missing = true; // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $t177 ) ) !== $t177 ) $missing = true; // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $tc ) )   !== $tc )   $missing = true; // phpcs:ignore WordPress.DB.DirectDatabaseQuery

    if ( $missing ) {
        ovpspo_activate();
    }
}

register_activation_hook( __FILE__, 'ovpspo_activate' );

/**
 * Creare tabele la activare plugin.
 */
function ovpspo_activate() {
    global $wpdb;
    $charset = $wpdb->get_charset_collate();
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';

    $t230 = $wpdb->prefix . 'f230_submissions';
    dbDelta( "CREATE TABLE IF NOT EXISTS $t230 (
        id             BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        data_trimitere DATETIME NOT NULL,
        nume           VARCHAR(100) NOT NULL,
        prenume        VARCHAR(100) NOT NULL,
        initiala       VARCHAR(5),
        cnp            VARCHAR(20) NOT NULL,
        email          VARCHAR(150),
        telefon        VARCHAR(30),
        judet          VARCHAR(60) NOT NULL,
        localitate     VARCHAR(100) NOT NULL,
        cod_postal     VARCHAR(10),
        strada         VARCHAR(150) NOT NULL,
        nr             VARCHAR(20) NOT NULL,
        bloc           VARCHAR(20),
        scara          VARCHAR(20),
        etaj           VARCHAR(10),
        apart          VARCHAR(20),
        perioada       TINYINT(1) NOT NULL DEFAULT 1,
        an_fiscal      SMALLINT(4) NOT NULL,
        gdpr           TINYINT(1) NOT NULL DEFAULT 0,
        pdf_path       VARCHAR(255),
        pdf_anaf_path  VARCHAR(255),
        ip             VARCHAR(45),
        PRIMARY KEY (id)
    ) $charset;" );

    $t177 = $wpdb->prefix . 'f177_submissions';
    dbDelta( "CREATE TABLE IF NOT EXISTS $t177 (
        id               BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        data_trimitere   DATETIME NOT NULL,
        an               SMALLINT(4) NOT NULL,
        data_inceput     DATE,
        data_sfarsit     DATE,
        rectificativa    TINYINT(1) DEFAULT 0,
        tip_platitor     VARCHAR(20),
        cif              VARCHAR(20) NOT NULL,
        denumire         VARCHAR(200) NOT NULL,
        judet            VARCHAR(60),
        localitate       VARCHAR(100),
        strada           VARCHAR(150),
        numar            VARCHAR(20),
        telefon          VARCHAR(30),
        email            VARCHAR(150),
        suma_max         DECIMAL(12,2) DEFAULT 0,
        suma_anterioara  DECIMAL(12,2) DEFAULT 0,
        suma_ramasa      DECIMAL(12,2) DEFAULT 0,
        tip_beneficiar   TINYINT(1) DEFAULT 1,
        bnf_denumire     VARCHAR(200),
        bnf_cif          VARCHAR(20),
        bnf_iban         VARCHAR(35),
        bnf_suma         DECIMAL(12,2) DEFAULT 0,
        bnf_gdpr         TINYINT(1) DEFAULT 0,
        imp_cif          VARCHAR(20),
        imp_denumire     VARCHAR(200),
        xml_path         VARCHAR(255),
        pdf_path         VARCHAR(255),
        ip               VARCHAR(45),
        PRIMARY KEY (id)
    ) $charset;" );

    $tc = $wpdb->prefix . 'contracte_sponsorizare';
    dbDelta( "CREATE TABLE IF NOT EXISTS $tc (
        id               BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        data_trimitere   DATETIME NOT NULL,
        nr_contract      VARCHAR(50),
        data_contract    DATE,
        tip_sponsor      VARCHAR(20),
        sponsor_nume     VARCHAR(200),
        sponsor_cui      VARCHAR(20),
        sponsor_email    VARCHAR(200),
        tip_sponsorizare VARCHAR(20),
        suma             DECIMAL(12,2) DEFAULT 0,
        pdf_path         VARCHAR(255),
        ip               VARCHAR(45),
        PRIMARY KEY (id)
    ) $charset;" );

    // OVPSPO_TCPDF bundled in lib/tcpdf/ - verifica si marcheaza statusul
    if ( file_exists( OVPSPO_TCPDF_PATH ) ) {
        update_option( 'ovpspo_tcpdf_status', 'ok' );
    } else {
        update_option( 'ovpspo_tcpdf_status', 'missing' );
    }
}
