<?php

declare(strict_types=1);

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

/**
 * 主数据：商品分类、单位、品牌、员工、仓库、客户、供应商、商品（含 DBShop 集成字段，合并原 230100）。
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('dberp_product_categories', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('分类编码');
            $table->string('name', 120)->comment('分类名称');
            $table->foreignId('parent_id')->nullable()->constrained('dberp_product_categories')->nullOnDelete();
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_prodcat_code');
            $table->index(['parent_id', 'sort_order'], 'idx_dberp_prodcat_parent_sort');
            $table->index(['is_enabled', 'sort_order'], 'idx_dberp_prodcat_enabled_sort');
        });

        Schema::create('dberp_units', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('单位编码');
            $table->string('name', 64)->comment('单位名称');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_unit_code');
            $table->unique('name', 'uq_dberp_unit_name');
            $table->index(['is_enabled', 'sort_order'], 'idx_dberp_unit_enabled_sort');
        });

        Schema::create('dberp_product_brands', function (Blueprint $table): void {
            $table->id();
            $table->string('brand_code', 64)->comment('品牌编码');
            $table->string('name', 120)->comment('品牌名称');
            $table->string('short_name', 64)->nullable()->comment('品牌简称');
            $table->string('logo', 512)->nullable()->comment('品牌标识（URL 或路径）');
            $table->text('description')->nullable()->comment('备注/说明');
            $table->boolean('is_enabled')->default(true)->comment('是否启用');
            $table->unsignedInteger('sort_order')->default(0)->comment('排序（越小越靠前）');
            $table->timestamps();

            $table->unique('brand_code', 'uq_dberp_product_brands_brand_code');
            $table->unique('name', 'uq_dberp_product_brands_name');
            $table->index(['is_enabled', 'sort_order', 'id'], 'idx_dberp_product_brands_enabled_sort');
        });

        Schema::create('dberp_employees', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('员工编码/工号');
            $table->string('name', 120)->comment('员工姓名');
            $table->string('mobile', 32)->nullable()->comment('手机');
            $table->string('email', 120)->nullable()->comment('邮箱');
            $table->string('department_name', 120)->nullable()->comment('部门预留');
            $table->string('position_name', 120)->nullable()->comment('岗位预留');
            $table->boolean('is_salesperson')->default(true)->comment('可作为业务员');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_emp_code');
            $table->index(['name', 'is_enabled'], 'idx_dberp_emp_name_enabled');
            $table->index(['is_salesperson', 'is_enabled'], 'idx_dberp_emp_sales_enabled');
        });

        Schema::create('dberp_warehouses', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('仓库编码');
            $table->string('name', 120)->comment('仓库名称');
            $table->string('warehouse_type', 32)->default('general')->comment('仓库类型');
            $table->string('address', 255)->nullable()->comment('地址');
            $table->string('country_code', 8)->nullable()->comment('国家代码');
            $table->foreignId('region_id')->nullable()->comment('地区节点')->constrained('dberp_regions')->nullOnDelete();
            $table->string('region_name_snapshot', 255)->nullable()->comment('地区展示快照');
            $table->foreignId('manager_employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
            $table->string('contact_phone', 32)->nullable()->comment('联系方式');
            $table->boolean('is_default')->default(false)->comment('默认仓');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_wh_code');
            $table->index(['warehouse_type', 'is_enabled'], 'idx_dberp_wh_type_enabled');
            $table->index(['is_default', 'is_enabled'], 'idx_dberp_wh_default_enabled');
            $table->index(['sort_order', 'id'], 'idx_dberp_wh_sort_id');
            $table->index(['country_code', 'region_id'], 'idx_dberp_wh_country_region');
        });

        Schema::create('dberp_customers', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('客户编码');
            $table->string('name', 160)->comment('客户名称');
            $table->string('contact_person', 120)->nullable()->comment('联系人');
            $table->string('mobile', 32)->nullable()->comment('手机');
            $table->string('phone', 32)->nullable()->comment('电话');
            $table->string('email', 120)->nullable()->comment('邮箱');
            $table->string('address', 255)->nullable()->comment('地址');
            $table->string('country_code', 8)->nullable()->comment('国家代码');
            $table->foreignId('region_id')->nullable()->comment('地区节点')->constrained('dberp_regions')->nullOnDelete();
            $table->string('region_name_snapshot', 255)->nullable()->comment('地区展示快照');
            $table->string('settlement_method', 32)->default('cash')->comment('默认结算方式');
            $table->foreignId('salesperson_employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_customer_code');
            $table->index(['name', 'is_enabled'], 'idx_dberp_customer_name_enabled');
            $table->index(['salesperson_employee_id', 'is_enabled'], 'idx_dberp_customer_sales_enabled');
            $table->index(['country_code', 'region_id'], 'idx_dberp_customer_country_region');

            $table->string('source_platform', 32)->nullable();
            $table->string('external_member_id', 128)->nullable();
            $table->string('integration_sync_status', 32)->nullable();
            $table->dateTime('last_integration_synced_at')->nullable();
            $table->index(['source_platform', 'external_member_id'], 'idx_dberp_cust_extmid');
        });

        Schema::create('dberp_suppliers', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('供应商编码');
            $table->string('name', 160)->comment('供应商名称');
            $table->string('contact_person', 120)->nullable()->comment('联系人');
            $table->string('mobile', 32)->nullable()->comment('手机');
            $table->string('phone', 32)->nullable()->comment('电话');
            $table->string('email', 120)->nullable()->comment('邮箱');
            $table->string('address', 255)->nullable()->comment('地址');
            $table->string('country_code', 8)->nullable()->comment('国家代码');
            $table->foreignId('region_id')->nullable()->comment('地区节点')->constrained('dberp_regions')->nullOnDelete();
            $table->string('region_name_snapshot', 255)->nullable()->comment('地区展示快照');
            $table->string('settlement_method', 32)->default('cash')->comment('默认结算方式');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_supplier_code');
            $table->index(['name', 'is_enabled'], 'idx_dberp_supplier_name_enabled');
            $table->index(['country_code', 'region_id'], 'idx_dberp_supplier_country_region');
        });

        Schema::create('dberp_products', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('商品编码');
            $table->string('name', 160)->comment('商品名称');
            $table->string('barcode', 64)->nullable()->comment('条码');
            $table->string('mnemonic_code', 64)->nullable()->comment('助记码');
            $table->foreignId('category_id')->nullable()->constrained('dberp_product_categories')->nullOnDelete();
            $table->foreignId('brand_id')->nullable()->constrained('dberp_product_brands')->restrictOnDelete()->comment('主档品牌');
            $table->string('brand_name', 120)->nullable()->comment('品牌承载位');
            $table->string('spec_model', 160)->nullable()->comment('规格型号承载位');
            $table->boolean('spec_enabled')->default(false)->comment('启用多规格维护');
            $table->foreignId('unit_id')->constrained('dberp_units')->restrictOnDelete();
            $table->boolean('is_auxiliary_unit_enabled')->default(false)->comment('是否启用辅助计量');
            $table->foreignId('auxiliary_unit_id')->nullable()->constrained('dberp_units')->restrictOnDelete()->comment('辅助计量单位');
            $table->decimal('auxiliary_unit_quantity', 18, 6)->nullable()->comment('辅助计量单位侧数量（换算关系）');
            $table->decimal('base_unit_quantity', 18, 6)->nullable()->comment('基本计量单位侧数量（换算关系）');
            $table->decimal('cost_price', 18, 4)->default(0)->comment('成本价基础');
            $table->decimal('sales_price', 18, 4)->default(0)->comment('销售价基础');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->boolean('can_purchase')->default(true)->comment('可采购');
            $table->boolean('can_sale')->default(true)->comment('可销售');
            $table->boolean('manages_inventory')->default(true)->comment('管库存');
            $table->boolean('manages_batch')->default(false)->comment('启用批次管理');
            $table->boolean('manages_serial_number')->default(false)->comment('启用序列号管理');
            $table->boolean('manages_expiry')->default(false)->comment('启用效期管理');
            $table->unsignedInteger('expiry_warning_days')->default(0)->comment('临期预警天数');
            $table->string('inventory_issue_policy', 32)->default('fifo')->comment('默认出库策略');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_product_code');
            $table->unique('barcode', 'uq_dberp_product_barcode');
            $table->index(['name', 'is_enabled'], 'idx_dberp_product_name_enabled');
            $table->index(['category_id', 'is_enabled'], 'idx_dberp_product_cat_enabled');
            $table->index(['unit_id', 'is_enabled'], 'idx_dberp_product_unit_enabled');
            $table->index(['mnemonic_code'], 'idx_dberp_product_mnemonic');
            $table->index(['sort_order', 'id'], 'idx_dberp_product_sort_id');
            $table->index(['manages_batch', 'is_enabled'], 'idx_dberp_prod_batch_enabled');
            $table->index(['manages_serial_number', 'is_enabled'], 'idx_dberp_prod_serial_enabled');
            $table->index(['manages_expiry', 'is_enabled'], 'idx_dberp_prod_expiry_enabled');
            $table->index(['spec_enabled', 'is_enabled'], 'idx_dberp_prod_spec_enabled');
            $table->index(['is_auxiliary_unit_enabled', 'unit_id'], 'idx_dberp_product_aux_enabled_unit');

            $table->string('source_platform', 32)->nullable();
            $table->string('external_product_id', 128)->nullable();
            $table->string('external_sku', 128)->nullable();
            $table->string('external_spec')->nullable();
            $table->string('integration_sync_status', 32)->nullable();
            $table->dateTime('last_integration_synced_at')->nullable();
            $table->index(['source_platform', 'external_product_id'], 'idx_dberp_prod_extid');
            $table->index(['source_platform', 'external_sku'], 'idx_dberp_prod_extsku');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_products');
        Schema::dropIfExists('dberp_suppliers');
        Schema::dropIfExists('dberp_customers');
        Schema::dropIfExists('dberp_warehouses');
        Schema::dropIfExists('dberp_employees');
        Schema::dropIfExists('dberp_product_brands');
        Schema::dropIfExists('dberp_units');
        Schema::dropIfExists('dberp_product_categories');
    }
};
