<?php

use Bookingor\Helper\HelperControl;
use Bookingor\Backend\Controller\Settings\SettingsControl;

/**
 * Fired during plugin activation
 *
 * @link       The Creative Next
 * @since      1.0.0
 *
 * @package    Bookingor
 * @subpackage Bookingor/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Bookingor
 * @subpackage Bookingor/includes
 * @author     The Creative Next <contact@thecreative-next.com>
 */
class Bookingor_Activator
{

  private static $bookingorDb;
  /**
   * Short Description. (use period)
   *
   * Long Description.
   *
   * @since    1.0.0
   */
  public static function activate()
  {
    ob_flush();
    ob_start();



    //category
    global $wpdb;
    $prefix = $wpdb->prefix . "bookingor_";


    $sql_cate = "CREATE TABLE  IF NOT EXISTS `{$prefix}categories` (
  
  `category_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `icon` VARCHAR(255) NOT NULL,
  `category_name` VARCHAR(255) NOT NULL,
  `status` tinyint(10) NOT NULL,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_cate);



    //service
    $sql_sr = "CREATE TABLE IF NOT EXISTS `{$prefix}services` (

  `service_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `service_icon` VARCHAR(255),
  `service_name` VARCHAR(255) NOT NULL,
  `service_description` TEXT,
  `category_get_id` INT(10) UNSIGNED,
  `service_cost` FLOAT(10,2) NOT NULL,
  `service_deposit` FLOAT(10,2),
  `service_deposit_type` VARCHAR(255),
  `service_tax` FLOAT(10,2),
  `service_duration` INT(11) NOT NULL,
  `service_duration_type` VARCHAR(255) NOT NULL,
  `service_duration_cuont` VARCHAR(255) NOT NULL,
   `service_duration_count` INT(11) NOT NULL,
  `timeslot_length` INT(11),
  `service_slot_increment` INT(11),
  `padding_before` INT(11),
  `padding_after` INT(11),
  `service_capacity_min` INT(11),
  `service_capacity_max` INT(11),
  `service_custom_cost` VARCHAR(750),
  `recurring_status` tinyint(4),
  `recurring_type` VARCHAR(255),
  `recurring_payment` VARCHAR(255),
  `service_cancel_booking_time` INT(11),
  `service_allow_booking_time` INT(11),
  `service_allow_zoom_meeting` INT(11),
  `single_booking_only_slot` tinyint(10),
  `service_time_sheet` VARCHAR(750), 
  `wc_id` INT(11),
  `status` tinyint(10) NOT NULL,
  FOREIGN KEY (`category_get_id`) REFERENCES {$prefix}categories(`category_id`) ON UPDATE CASCADE ON DELETE CASCADE, 
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_sr);

    $sql_sr_tsheet = "CREATE TABLE IF NOT EXISTS `{$prefix}timesheet` ( 
  `time_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `service_get_id` INT(10) UNSIGNED,
  `team_id` INT(10) UNSIGNED,
  `time_sheet_weekly` VARCHAR(750), 
   FOREIGN KEY (`service_get_id`) REFERENCES {$prefix}services(`service_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_sr_tsheet);


    //services
    $sql_staff = "CREATE TABLE IF NOT EXISTS `{$prefix}staff_add` ( 
  `staff_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `staff_first_name` VARCHAR(255) NOT NULL,
  `staff_last_name` VARCHAR(255) NOT NULL,
  `staff_email` VARCHAR(255) NOT NULL,
  `staff_phone` VARCHAR(255),
  `staff_location` VARCHAR(255),
  `staff_icon` VARCHAR(255),
  `staff_tag_line` VARCHAR(255),
  `staff_about` VARCHAR(255),
  `staff_sheet_weekly` VARCHAR(755),
  `staff_holiday` VARCHAR(255),
  `google_calendar_id` VARCHAR(255),
  `google_access_token` TEXT(755),
  `zoom_user_id` VARCHAR(555),
  `google_time_zone` VARCHAR(255),
  `status` tinyint(10) NOT NULL,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_staff);




    $notification = "CREATE TABLE IF NOT EXISTS `{$prefix}notifications` ( 
  `id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `name_action` VARCHAR(255),
  `name` VARCHAR(255),
  `subject` VARCHAR(255),
  `text` VARCHAR(750),
  `send_to` VARCHAR(255),
  `type` VARCHAR(255),
  `paid_amount` FLOAT(10,2),
  `tax_charges` FLOAT(10,2),
  `coupon_id` INT(11),
  `book_date` DATE,
  `book_time` TIME,
  `send_on_time` TIME NULL DEFAULT NULL,
  `status` tinyint(10) NOT NULL,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($notification);

    $notification_log = "CREATE TABLE IF NOT EXISTS `{$prefix}notification_log` ( 
  `id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `name_action` VARCHAR(255),
  `notification_get_id` INT(11),
  `customer_get_id` INT(11),
  `employee_get_id` INT(11),
  `appoint_get_id` INT(11),
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($notification_log);




    $sql_staff_assign_service = "CREATE TABLE IF NOT EXISTS `{$prefix}staff_assign_service` ( 
      `assign_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      `staff_get_id` INT(10) UNSIGNED,
      `service_get_id` INT(10) UNSIGNED,
      `staff_price` FLOAT(10,2),
      `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      )" . $wpdb->get_charset_collate();
    dbDelta($sql_staff_assign_service);

    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_staff);

    $sql_location_assign_service = "CREATE TABLE IF NOT EXISTS `{$prefix}location_assign_service` ( 
      `location_assign_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      `location_get_id` INT(10) UNSIGNED,
      `service_get_id` INT(10) UNSIGNED,
      `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      )" . $wpdb->get_charset_collate();
    dbDelta($sql_location_assign_service);

    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_staff);

    $sql_location_assign_staff = "CREATE TABLE IF NOT EXISTS `{$prefix}location_assign_staff` ( 
      `location_assign_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      `location_get_id` INT(10) UNSIGNED,
      `staff_get_id` INT(10) UNSIGNED,
      `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
      )" . $wpdb->get_charset_collate();
    dbDelta($sql_location_assign_staff);

    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($sql_staff);

    $location = "CREATE TABLE IF NOT EXISTS `{$prefix}location` ( 
  `location_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `location_title` VARCHAR(255),
  `location_icon` VARCHAR(255),
  `location_country` VARCHAR(255),
  `location_city` VARCHAR(255),
  `location_address` VARCHAR(255),
  `location_phone` VARCHAR(255),
  `location_latitude` VARCHAR(255),
  `location_longitude` VARCHAR(255),
  `location_time_sheet` VARCHAR(750),
  `status` VARCHAR(255),
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($location);


    $bookedAppointmentsQuery = "CREATE TABLE IF NOT EXISTS `{$prefix}booked_appointment` (
  `book_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `customer_get_id` INT(10) UNSIGNED,
  `duration_service` INT(10),
  `start_time` TIME,
  `end_time` TIME,
  `picked_date_end` DATE,
  `picked_date` DATE,
  `people_count` INT(11),
  `service_get_price` FLOAT(10,2) NOT NULL,
  `extra_get_price` FLOAT(10,2),
  `discount_get_price` FLOAT(10,2),
  `tax_get_price` FLOAT(10,2),
  `paid_amount` FLOAT(10,2) NOT NULL,
  `payment_method` VARCHAR(255) NOT NULL,
  `customer_time_zone` VARCHAR(255) NOT NULL,
  `token_code` VARCHAR(255) NOT NULL,
  `appoint_status` VARCHAR(255),
  `category_get_id` INT(10) UNSIGNED,
  `service_get_id` INT(10) UNSIGNED,
  `staff_get_id` INT(10) UNSIGNED,
  `location_get_id` INT(10) UNSIGNED,
  `zoom_meeting_id` VARCHAR(750),
  FOREIGN KEY (`category_get_id`) REFERENCES {$prefix}categories(`category_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY (`service_get_id`) REFERENCES {$prefix}services(`service_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY (`staff_get_id`) REFERENCES {$prefix}staff_add(`staff_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($bookedAppointmentsQuery);

    // If the services table already exists but doesn't have the new column, alter the table to add it
    $check_column = $wpdb->get_results("SHOW COLUMNS FROM `{$prefix}services` LIKE 'service_duration_count'");
    if (empty($check_column)) {
      $wpdb->query("ALTER TABLE `{$prefix}services` ADD `service_duration_count` INT(11) NOT NULL AFTER `service_duration_type`");
    }

    $check_notification_column = $wpdb->get_results("SHOW COLUMNS FROM `{$prefix}notifications` LIKE 'send_on_time'");
    if (empty($check_notification_column)) {
      $wpdb->query("ALTER TABLE `{$prefix}notifications` ADD `send_on_time` TIME NULL DEFAULT NULL AFTER `book_time`");
    }

    // If the google meet


    $customers = "CREATE TABLE IF NOT EXISTS `{$prefix}customer_add` ( 
  `customer_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `book_get_id` INT(10) UNSIGNED,
  `customer_first_name` VARCHAR(255) NOT NULL,
  `customer_last_name` VARCHAR(255) NOT NULL,
  `customer_email` VARCHAR(255) NOT NULL,
  `customer_phone` VARCHAR(255),
  `customer_gender` VARCHAR(255) NOT NULL,
  `customer_location` VARCHAR(255) NOT NULL,
  `customer_description` VARCHAR(255) NOT NULL,
  `customer_image` VARCHAR(255),
  `google_user_data` TEXT(755),
  `google_calendar_id` VARCHAR(225),
  `google_access_token` TEXT(755),
  `google_time_zone` VARCHAR(255),
  `status` tinyint(10) NOT NULL,
  FOREIGN KEY (`book_get_id`) REFERENCES {$prefix}booked_appointment(`book_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($customers);


    $customer_payments = "CREATE TABLE IF NOT EXISTS `{$prefix}customers_payment` ( 
  `pay_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  `book_get_id` INT(10) UNSIGNED,
  `customer_get_id` INT(10) UNSIGNED,
  `total_cost` FLOAT(10,2),
  `payment_method` VARCHAR(255),
  `paid_amount` FLOAT(10,2),
  `due_amount` FLOAT(10,2),
  `tax_charges` FLOAT(10,2),
  `coupon_id` INT(11),
  `payment_status` VARCHAR(255),
  FOREIGN KEY (`customer_get_id`) REFERENCES {$prefix}customer_add(`customer_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  FOREIGN KEY (`book_get_id`) REFERENCES {$prefix}booked_appointment(`book_id`) ON UPDATE CASCADE ON DELETE CASCADE,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )" . $wpdb->get_charset_collate();
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");
    dbDelta($customer_payments);


    add_role(
      'bookingor_staff',
      __('Bookingor Staff', "bookingor"),
      array(
        'read' => true,  // Example capabilities, adjust as needed
        'edit_posts' => false,
        // Add other capabilities as needed
      )
    );

    add_role(
      'bookingor_customer',
      __('Bookingor Customer', "bookingor"),
      array(
        'read' => true,  // Example capabilities, adjust as needed
        'edit_posts' => false,
        // Add other capabilities as needed
      )
    );
  }

  public static function email_data_insert()
  {


    global $wpdb;
    self::$bookingorDb = $wpdb;

    $table_name = $wpdb->prefix . "bookingor_notifications";


    $customerEmails = HelperControl::email_seeder_customer();
    $staffEmails = HelperControl::email_seeder_employee();

    $existingData = self::$bookingorDb->get_results("SELECT id, name_action, name, subject, text, send_to, type, paid_amount, tax_charges, coupon_id, book_date, book_time, status FROM {$wpdb->prefix}bookingor_notifications where type = 'email'");

    $existingDataSMS = self::$bookingorDb->get_results("SELECT id, name_action, name, subject, text, send_to, type, paid_amount, tax_charges, coupon_id, book_date, book_time, status FROM {$wpdb->prefix}bookingor_notifications WHERE type = 'sms'");

    $existingDataWhatsapp = self::$bookingorDb->get_results("SELECT id, name_action, name, subject, text, send_to, type, paid_amount, tax_charges, coupon_id, book_date, book_time, status FROM {$wpdb->prefix}bookingor_notifications WHERE type = 'whatsapp'");

    if (empty($existingData)) {
      // Data does not exist, insert it
      foreach ($customerEmails as $data) {
        self::$bookingorDb->insert($table_name, $data);
      }

      foreach ($staffEmails as $data) {
        self::$bookingorDb->insert($table_name, $data);
      }
    }
  }

  public static function default_settings_insert()
  {
    SettingsControl::Adds_Options();
  }
}
