<?php

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

class AIEO_Option_Groups_Schema {

	const OPTION_VERSION = 'aieo_option_groups_schema_version';
	const SCHEMA_VERSION = '1.0.0';

	
	public static function install() {
		global $wpdb;
		$charset_collate = $wpdb->get_charset_collate();
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$groups_tbl  = $wpdb->prefix . 'aieo_option_groups';
		$options_tbl = $wpdb->prefix . 'aieo_options';
		$assign_tbl  = $wpdb->prefix . 'aieo_product_option_groups';

		
		
		
		
		
		
		$sql_groups = "CREATE TABLE {$groups_tbl} (
			id           bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			slug         varchar(64)         NOT NULL,
			name         varchar(160)        NOT NULL,
			select_type  varchar(12)         NOT NULL DEFAULT 'checkbox',
			min_select   smallint(5) unsigned NOT NULL DEFAULT 0,
			max_select   smallint(5) unsigned NOT NULL DEFAULT 0,
			included_free smallint(5) unsigned NOT NULL DEFAULT 0,
			free_policy  varchar(16)         NOT NULL DEFAULT 'expensive_free',
			description  varchar(255)        NULL,
			sort_order   int(11)             NOT NULL DEFAULT 0,
			enabled      tinyint(1)          NOT NULL DEFAULT 1,
			created_at   datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at   datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY uk_slug (slug),
			KEY ix_enabled (enabled)
		) {$charset_collate};";

		
		
		
		
		
		
		
		
		
		$sql_options = "CREATE TABLE {$options_tbl} (
			id                bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			group_id          bigint(20) unsigned NOT NULL,
			slug              varchar(64)         NOT NULL,
			label             varchar(160)        NOT NULL,
			price_delta       decimal(12,4)       NOT NULL DEFAULT 0.0000,
			default_selected  tinyint(1)          NOT NULL DEFAULT 0,
			exclude_from_allowance tinyint(1)     NOT NULL DEFAULT 0,
			linked_product_id bigint(20) unsigned NOT NULL DEFAULT 0,
			sku               varchar(64)         NULL,
			availability      varchar(12)         NOT NULL DEFAULT 'auto',
			sort_order        int(11)             NOT NULL DEFAULT 0,
			enabled           tinyint(1)          NOT NULL DEFAULT 1,
			created_at        datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at        datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY uk_group_slug (group_id, slug),
			KEY ix_group_sort (group_id, sort_order),
			KEY ix_linked (linked_product_id),
			KEY ix_sku (sku),
			KEY ix_availability (availability)
		) {$charset_collate};";

		
		
		
		
		$sql_assign = "CREATE TABLE {$assign_tbl} (
			id          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			product_id  bigint(20) unsigned NOT NULL,
			group_id    bigint(20) unsigned NOT NULL,
			sort_order  int(11)             NOT NULL DEFAULT 0,
			enabled     tinyint(1)          NOT NULL DEFAULT 1,
			created_at  datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY uk_product_group (product_id, group_id),
			KEY ix_product_sort (product_id, sort_order),
			KEY ix_group (group_id)
		) {$charset_collate};";

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		$prices_tbl = $wpdb->prefix . 'aieo_option_prices';
		$sql_prices = "CREATE TABLE {$prices_tbl} (
			id                  bigint(20) unsigned NOT NULL AUTO_INCREMENT,
			option_id           bigint(20) unsigned NOT NULL,
			scope_assignment_id bigint(20) unsigned NOT NULL DEFAULT 0,
			when_group_id       bigint(20) unsigned NOT NULL DEFAULT 0,
			when_option_id      bigint(20) unsigned NOT NULL DEFAULT 0,
			price_delta         decimal(12,4)       NOT NULL DEFAULT 0.0000,
			created_at          datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at          datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY uk_rule (option_id, scope_assignment_id, when_group_id, when_option_id),
			KEY ix_option (option_id),
			KEY ix_scope (scope_assignment_id),
			KEY ix_when (when_group_id, when_option_id)
		) {$charset_collate};";

		dbDelta( $sql_groups );
		dbDelta( $sql_options );
		dbDelta( $sql_assign );
		dbDelta( $sql_prices );

		update_option( self::OPTION_VERSION, self::SCHEMA_VERSION, false );
	}

	
	public static function maybe_install() {
		if ( get_option( self::OPTION_VERSION ) !== self::SCHEMA_VERSION ) {
			self::install();
		}
	}

	
	public static function tables() {
		global $wpdb;
		return array(
			'groups'      => $wpdb->prefix . 'aieo_option_groups',
			'options'     => $wpdb->prefix . 'aieo_options',
			'assignments' => $wpdb->prefix . 'aieo_product_option_groups',
			'prices'      => $wpdb->prefix . 'aieo_option_prices',
		);
	}
}
