<?php
/**
 * نصب Premium فروشگاه عرقیات — PDP، نظرات، تنظیمات، رسانه
 * php run-arag-premium-install.php
 */
declare(strict_types=1);

define( 'ACC_BASE_PATH', __DIR__ );
require_once __DIR__ . '/standalone/bootstrap.php';

header( 'Content-Type: application/json; charset=utf-8' );
global $wpdb;
$pre     = $wpdb->prefix;
$charset = 'DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
$done    = [];

function arag_col_exists( $wpdb, string $table, string $col ): bool {
    $row = $wpdb->get_row( "SHOW COLUMNS FROM `{$table}` LIKE '{$col}'" );
    return (bool) $row;
}

function arag_add_col( $wpdb, string $table, string $col, string $ddl, array &$done ): void {
    if ( ! arag_col_exists( $wpdb, $table, $col ) ) {
        $wpdb->query( "ALTER TABLE `{$table}` ADD COLUMN {$ddl}" );
        $done[] = "add {$table}.{$col}";
    }
}

// ── families columns ──
$ft = $pre . 'arag_families';
arag_add_col( $wpdb, $ft, 'tagline', "tagline VARCHAR(240) NOT NULL DEFAULT '' AFTER icon", $done );
arag_add_col( $wpdb, $ft, 'description_short', "description_short TEXT NULL AFTER tagline", $done );
arag_add_col( $wpdb, $ft, 'description_long', "description_long TEXT NULL AFTER description_short", $done );
arag_add_col( $wpdb, $ft, 'bottle_image_url', "bottle_image_url VARCHAR(500) NOT NULL DEFAULT '' AFTER description_long", $done );
arag_add_col( $wpdb, $ft, 'gallery_json', "gallery_json JSON NULL AFTER bottle_image_url", $done );

// ── variants image ──
$vt = $pre . 'arag_variants';
arag_add_col( $wpdb, $vt, 'image_url', "image_url VARCHAR(500) NOT NULL DEFAULT '' AFTER description", $done );

// ── shop settings ──
$wpdb->query( "CREATE TABLE IF NOT EXISTS {$pre}arag_shop_settings (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    hero_eyebrow VARCHAR(200) NOT NULL DEFAULT '',
    hero_title VARCHAR(300) NOT NULL DEFAULT '',
    hero_subtitle TEXT NULL,
    hero_video_url VARCHAR(500) NOT NULL DEFAULT '',
    hero_poster_url VARCHAR(500) NOT NULL DEFAULT '',
    brand_story TEXT NULL,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) {$charset}" );
$done[] = 'arag_shop_settings';

// ── reviews ──
$wpdb->query( "CREATE TABLE IF NOT EXISTS {$pre}arag_reviews (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    family_id BIGINT UNSIGNED NOT NULL,
    author_name VARCHAR(120) NOT NULL DEFAULT '',
    author_city VARCHAR(80) NOT NULL DEFAULT '',
    rating TINYINT UNSIGNED NOT NULL DEFAULT 5,
    body TEXT NOT NULL,
    is_verified TINYINT(1) NOT NULL DEFAULT 0,
    is_approved TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY family_id (family_id)
) {$charset}" );
$done[] = 'arag_reviews';

// ── uploads dir ──
$uploadDir = __DIR__ . '/uploads/arag';
$assetDir  = __DIR__ . '/assets/img/shop/bottles';
foreach ( [ $uploadDir, $assetDir ] as $d ) {
    if ( ! is_dir( $d ) ) {
        mkdir( $d, 0755, true );
        $done[] = 'mkdir ' . basename( dirname( $d ) ) . '/' . basename( $d );
    }
}

// placeholder htaccess for uploads
$ht = $uploadDir . '/.htaccess';
if ( ! file_exists( $ht ) ) {
    file_put_contents( $ht, "Options -Indexes\n<FilesMatch \"\\.(php|phtml)$\">\nDeny from all\n</FilesMatch>\n" );
}

echo json_encode( [
    'success' => true,
    'changes' => $done,
    'tables'  => [
        'arag_shop_settings' => $wpdb->get_var( "SHOW TABLES LIKE '{$pre}arag_shop_settings'" ) === $pre . 'arag_shop_settings',
        'arag_reviews'       => $wpdb->get_var( "SHOW TABLES LIKE '{$pre}arag_reviews'" ) === $pre . 'arag_reviews',
    ],
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
