<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        // 收支分类（树形）
        Schema::create('finance_categories', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('parent_id')->default(0)->index();
            $table->string('name', 50);
            $table->tinyInteger('type')->default(1)->comment('1=收入 2=支出');
            $table->tinyInteger('level')->default(1);
            $table->integer('sort')->default(0);
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 收支记录
        Schema::create('finance_records', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->string('record_no', 50)->unique();
            $table->unsignedBigInteger('category_id')->nullable();
            $table->tinyInteger('type')->comment('1=收入 2=支出');
            $table->decimal('amount', 12, 2);
            $table->tinyInteger('pay_method')->nullable()->comment('1=现金 2=微信 3=支付宝 4=银行');
            $table->string('related_type', 50)->nullable()->comment('关联类型');
            $table->unsignedBigInteger('related_id')->nullable();
            $table->string('description')->nullable();
            $table->tinyInteger('audit_status')->default(0)->comment('0=待审核 1=已通过 2=已驳回');
            $table->unsignedBigInteger('audit_user_id')->nullable();
            $table->timestamp('audit_at')->nullable();
            $table->unsignedBigInteger('operator_id')->nullable();
            $table->date('record_date');
            $table->timestamps();
            $table->index(['store_id', 'type']);
            $table->index('record_date');
        });

        // 客户账户
        Schema::create('customer_accounts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('customer_id')->index();
            $table->decimal('cash_balance', 12, 2)->default(0);
            $table->decimal('card_balance', 12, 2)->default(0);
            $table->integer('points')->default(0);
            $table->timestamps();
            $table->unique(['store_id', 'customer_id']);
        });

        // 账户流水
        Schema::create('account_transactions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('customer_account_id')->index();
            $table->unsignedBigInteger('customer_id')->index();
            $table->tinyInteger('type')->comment('1=充值 2=消费 3=退款 4=积分变动');
            $table->decimal('amount', 12, 2);
            $table->decimal('balance_after', 12, 2);
            $table->string('related_type', 50)->nullable();
            $table->unsignedBigInteger('related_id')->nullable();
            $table->string('description')->nullable();
            $table->unsignedBigInteger('operator_id')->nullable();
            $table->timestamp('created_at')->nullable();
        });

        // 储值卡种
        Schema::create('prepaid_cards', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->string('name', 100);
            $table->decimal('face_value', 10, 2);
            $table->decimal('sell_price', 10, 2);
            $table->decimal('discount_rate', 5, 2)->default(1.00);
            $table->integer('validity_days')->default(365);
            $table->tinyInteger('status')->default(1);
            $table->integer('sort')->default(0);
            $table->timestamps();
        });

        // 发票
        Schema::create('invoices', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->string('invoice_no', 50)->unique();
            $table->unsignedBigInteger('customer_id')->nullable();
            $table->tinyInteger('type')->default(1)->comment('1=电子 2=纸质');
            $table->decimal('amount', 12, 2);
            $table->decimal('tax_amount', 10, 2)->default(0);
            $table->string('title')->nullable();
            $table->string('tax_no', 50)->nullable();
            $table->string('related_type', 50)->nullable();
            $table->unsignedBigInteger('related_id')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0=待开 1=已开 2=已红冲');
            $table->unsignedBigInteger('operator_id')->nullable();
            $table->timestamp('issued_at')->nullable();
            $table->timestamps();
            $table->index(['store_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('invoices');
        Schema::dropIfExists('prepaid_cards');
        Schema::dropIfExists('account_transactions');
        Schema::dropIfExists('customer_accounts');
        Schema::dropIfExists('finance_records');
        Schema::dropIfExists('finance_categories');
    }
};
