<?php
declare(strict_types=1);

use Celena\Core\Database\Blueprint;
use Celena\Core\Database\Connection;
use Celena\Core\Database\Schema;

return function (Schema $schema, Connection $conn): void {
    $schema->create('shop_orders', function (Blueprint $t) {
        $t->id();
        $t->string('number', 32);
        $t->bigInteger('user_id')->nullable();
        $t->string('customer_name', 191);
        $t->string('customer_email', 191);
        $t->string('customer_phone', 32)->nullable();
        $t->text('customer_address')->nullable();
        $t->text('comment')->nullable();
        $t->decimal('total', 12, 2)->default(0);
        $t->string('currency', 8)->default('USD');
        $t->string('status', 32)->default('new'); // new, paid, shipped, delivered, cancelled
        $t->string('payment_method', 32)->nullable();
        $t->json('shipping')->nullable();
        $t->json('meta')->nullable();
        $t->datetime('created_at');
        $t->datetime('updated_at')->nullable();
        $t->unique('number');
        $t->index('user_id');
        $t->index('status');
    });

    $schema->create('shop_order_items', function (Blueprint $t) {
        $t->id();
        $t->bigInteger('order_id');
        $t->bigInteger('product_id');
        $t->string('product_title', 255);
        $t->string('product_sku', 64)->nullable();
        $t->decimal('price', 12, 2);
        $t->integer('quantity')->default(1);
        $t->decimal('subtotal', 12, 2);
        $t->index('order_id');
        $t->index('product_id');
    });
};
