<?php
/**
 * Plugin Name: Plugin Compass Form Builder
 * Plugin URI: https://plugincompass.com/plugins/pc-form-builder-xyz123
 * Update URI: false
 * Description: A complete form builder plugin with form builder, all forms management, and responses tracking.
 * Version: 1.0.0
 * Author: Plugin Compass
 * Author URI: https://plugincompass.com
 * Text Domain: pc-form-builder-xyz123
 * Domain Path: /languages
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @package PCFormBuilder
 */

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

define( 'PCFB_VERSION', '1.0.0' );
define( 'PCFB_PLUGIN_FILE', __FILE__ );
define( 'PCFB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'PCFB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PCFB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

require_once PCFB_PLUGIN_DIR . 'includes/class-db-migration.php';
require_once PCFB_PLUGIN_DIR . 'includes/class-form-handler.php';
require_once PCFB_PLUGIN_DIR . 'admin/admin-menu.php';

class PC_Form_Builder {

    private static $instance = null;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'init', array( $this, 'init' ) );
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_public_assets' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
    }

    public function init() {
        load_plugin_textdomain( 'pc-form-builder-xyz123', false, dirname( PCFB_PLUGIN_BASENAME ) . '/languages' );

        PC_Form_Handler::get_instance();
        PCFB_Admin_Menu::get_instance();
    }

    public function enqueue_public_assets() {
        wp_enqueue_style(
            'pcfb-public-style',
            PCFB_PLUGIN_URL . 'public/css/public-style.css',
            array(),
            PCFB_VERSION
        );

        wp_enqueue_script(
            'pcfb-public-script',
            PCFB_PLUGIN_URL . 'public/js/public-script.js',
            array( 'jquery' ),
            PCFB_VERSION,
            true
        );

        wp_localize_script( 'pcfb-public-script', 'pcfbVars', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'nonce'   => wp_create_nonce( 'pcfb_public_nonce' ),
        ) );
    }

    public function enqueue_admin_assets() {
        $screen = get_current_screen();

        if ( 'toplevel_page_pcfb-form-builder' === $screen->id ||
             'form-builder_page_pcfb-all-forms' === $screen->id ||
             'form-builder_page_pcfb-responses' === $screen->id ) {

            wp_enqueue_style(
                'pcfb-admin-style',
                PCFB_PLUGIN_URL . 'admin/css/admin-style.css',
                array(),
                PCFB_VERSION
            );

            wp_enqueue_script(
                'pcfb-admin-script',
                PCFB_PLUGIN_URL . 'admin/js/admin-script.js',
                array( 'jquery', 'jquery-ui-sortable', 'jquery-ui-draggable' ),
                PCFB_VERSION,
                true
            );

            wp_localize_script( 'pcfb-admin-script', 'pcfbAdminVars', array(
                'ajaxurl' => admin_url( 'admin-ajax.php' ),
                'nonce'   => wp_create_nonce( 'pcfb_admin_nonce' ),
                'i18n'    => array(
                    'confirmDelete' => __( 'Are you sure you want to delete this form?', 'pc-form-builder-xyz123' ),
                    'confirmDeleteField' => __( 'Are you sure you want to delete this field?', 'pc-form-builder-xyz123' ),
                    'fieldLabel' => __( 'Field Label', 'pc-form-builder-xyz123' ),
                    'fieldPlaceholder' => __( 'Placeholder text', 'pc-form-builder-xyz123' ),
                    'required' => __( 'Required', 'pc-form-builder-xyz123' ),
                    'delete' => __( 'Delete', 'pc-form-builder-xyz123' ),
                ),
            ) );
        }
    }
}

register_activation_hook( __FILE__, array( 'PC_DB_Migration', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'PC_DB_Migration', 'deactivate' ) );
register_uninstall_hook( __FILE__, array( 'PC_Form_Builder', 'uninstall' ) );

add_action( 'plugins_loaded', array( 'PC_Form_Builder', 'get_instance' ) );

add_filter( 'site_transient_update_plugins', function( $value ) {
    $plugin_file = plugin_basename( __FILE__ );
    if ( isset( $value->response[ $plugin_file ] ) ) {
        unset( $value->response[ $plugin_file ] );
    }
    return $value;
} );
