<?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('approval_templates', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->string('name', 100);
            $table->tinyInteger('type')->comment('1=请假 2=采购 3=合同 4=退款 5=换房');
            $table->json('form_fields')->nullable();
            $table->json('flow_nodes')->nullable();
            $table->tinyInteger('status')->default(1);
            $table->integer('sort')->default(0);
            $table->timestamps();
        });

        // 审批记录
        Schema::create('approvals', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('template_id')->nullable();
            $table->string('title');
            $table->unsignedBigInteger('applicant_id')->index();
            $table->json('form_data')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0=待审批 1=审批中 2=已通过 3=已驳回 4=已撤回');
            $table->integer('current_node')->default(0);
            $table->string('related_type', 50)->nullable();
            $table->unsignedBigInteger('related_id')->nullable();
            $table->timestamps();
        });

        // 审批节点
        Schema::create('approval_nodes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('approval_id')->index();
            $table->integer('node_order')->default(0);
            $table->unsignedBigInteger('approver_id');
            $table->tinyInteger('action')->default(0)->comment('0=待处理 1=通过 2=驳回');
            $table->string('comment')->nullable();
            $table->timestamp('acted_at')->nullable();
            $table->timestamp('created_at')->nullable();
        });

        // 公告
        Schema::create('announcements', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->string('title');
            $table->text('content');
            $table->tinyInteger('type')->default(1)->comment('1=通知 2=公告 3=制度');
            $table->boolean('is_top')->default(false);
            $table->tinyInteger('publish_status')->default(0)->comment('0=草稿 1=已发布');
            $table->timestamp('published_at')->nullable();
            $table->unsignedBigInteger('author_id')->nullable();
            $table->timestamps();
        });

        // 交接班
        Schema::create('handovers', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->date('shift_date');
            $table->tinyInteger('shift_type')->comment('1=早班 2=中班 3=晚班 4=夜班');
            $table->unsignedBigInteger('handover_user_id');
            $table->unsignedBigInteger('receiver_user_id')->nullable();
            $table->text('content');
            $table->text('issues')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0=待确认 1=已确认');
            $table->timestamp('confirmed_at')->nullable();
            $table->timestamp('created_at')->nullable();
        });

        // 消息通知
        Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->string('title');
            $table->string('content')->nullable();
            $table->tinyInteger('type')->default(1)->comment('1=系统 2=审批 3=待办 4=提醒');
            $table->string('related_type', 50)->nullable();
            $table->unsignedBigInteger('related_id')->nullable();
            $table->boolean('is_read')->default(false);
            $table->timestamp('read_at')->nullable();
            $table->timestamp('created_at')->nullable();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('notifications');
        Schema::dropIfExists('handovers');
        Schema::dropIfExists('announcements');
        Schema::dropIfExists('approval_nodes');
        Schema::dropIfExists('approvals');
        Schema::dropIfExists('approval_templates');
    }
};
