<?php

/**
 * Fired during plugin activation
 *
 * @link       https://www.wpsaad.com
 * @since      1.0.0
 *
 * @package    ODDW
 * @subpackage ODDW/includes
 */
/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    ODDW
 * @subpackage ODDW/includes
 * @author     WPSAAD 
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class ODDW_Activator {
    /**
     * Plugin activation tasks: add driver role and register driver meta fields.
     *
     * @since 1.0.0
     */
    public static function activate() {
        // Create Delivery Driver user role
        add_role( 'driver', 'Delivery Driver', array(
            'read'         => true,
            'edit_posts'   => false,
            'delete_posts' => false,
        ) );
        // Register driver meta fields
        self::register_driver_meta_fields();
        // Create default shortcode pages if not present
        if ( function_exists( 'is_admin' ) ) {
            self::create_shortcode_pages();
        }
    }

    /**
     * Register driver meta fields for image, car image, and car type.
     */
    public static function register_driver_meta_fields() {
        register_meta( 'user', 'oddw_driver_image', array(
            'show_in_rest'  => true,
            'type'          => 'string',
            'description'   => 'Driver profile image (attachment ID or URL)',
            'single'        => true,
            'auth_callback' => function () {
                return current_user_can( 'edit_users' );
            },
        ) );
        register_meta( 'user', 'oddw_driver_car_image', array(
            'show_in_rest'  => true,
            'type'          => 'string',
            'description'   => 'Driver car image (attachment ID or URL)',
            'single'        => true,
            'auth_callback' => function () {
                return current_user_can( 'edit_users' );
            },
        ) );
        register_meta( 'user', 'oddw_driver_car_type', array(
            'show_in_rest'  => true,
            'type'          => 'string',
            'description'   => 'Driver car type',
            'single'        => true,
            'auth_callback' => function () {
                return current_user_can( 'edit_users' );
            },
        ) );
    }

    /**
     * Create pages for plugin shortcodes on activation.
     */
    public static function create_shortcode_pages() {
        if ( !function_exists( 'wp_insert_post' ) ) {
            require_once ABSPATH . 'wp-admin/includes/post.php';
        }
        // Load helpers if available
        $helpers = dirname( __FILE__ ) . '/functions/oddw-functions.php';
        if ( file_exists( $helpers ) ) {
            require_once $helpers;
        }
        $types = array(
            'dashboard' => array(
                'title'     => __( 'Driver Dashboard', 'orders-delivery-drivers-for-woocommerce' ),
                'shortcode' => '[oddw_delivery_drivers_dashboard]',
            ),
        );
        foreach ( $types as $type => $data ) {
            $existing_id = ( function_exists( 'oddw_get_page_id' ) ? oddw_get_page_id( $type ) : null );
            if ( $existing_id && get_post( $existing_id ) ) {
                continue;
                // already configured and exists
            }
            // create page
            $postarr = array(
                'post_title'   => $data['title'],
                'post_content' => $data['shortcode'],
                'post_status'  => 'publish',
                'post_type'    => 'page',
            );
            $page_id = wp_insert_post( $postarr );
            if ( is_wp_error( $page_id ) || !$page_id ) {
                continue;
            }
            // store option
            if ( function_exists( 'oddw_set_page_id' ) ) {
                oddw_set_page_id( $type, $page_id );
            } else {
                update_option( 'oddw_settings_' . $type . '_page_id', absint( $page_id ) );
            }
        }
    }

    /**
     * Create driver applications table
     *
     * @since 2.4.2
     */
    public static function create_driver_applications_table() {
        global $wpdb;
        $table_name = $wpdb->prefix . 'oddw_driver_applications';
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE {$table_name} (\r\n\t\t\tid mediumint(9) NOT NULL AUTO_INCREMENT,\r\n\t\t\tname varchar(100) NOT NULL,\r\n\t\t\temail varchar(100) NOT NULL,\r\n\t\t\tphone varchar(20) NOT NULL,\r\n\t\t\tlicense varchar(50) NOT NULL,\r\n\t\t\tvehicle varchar(200) NOT NULL,\r\n\t\t\texperience varchar(20) NOT NULL,\r\n\t\t\tavailability varchar(50) NOT NULL,\r\n\t\t\tmessage text,\r\n\t\t\tstatus varchar(20) DEFAULT 'pending',\r\n\t\t\tcreated_at datetime DEFAULT CURRENT_TIMESTAMP,\r\n\t\t\tPRIMARY KEY (id),\r\n\t\t\tUNIQUE KEY email (email)\r\n\t\t) {$charset_collate};";
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta( $sql );
    }

}
