<?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('channels', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->string('name', 50);
            $table->tinyInteger('type')->nullable()->comment('1线上 2线下 3转介绍');
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 线索
        Schema::create('leads', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->unsignedBigInteger('channel_id')->nullable();
            $table->string('name', 50);
            $table->string('phone', 20);
            $table->string('wechat', 50)->nullable();
            $table->date('expected_date')->nullable()->comment('预产期');
            $table->string('source', 50)->nullable();
            $table->tinyInteger('status')->default(1)->comment('1新建 2跟进中 3已转化 4无效');
            $table->unsignedBigInteger('owner_id')->nullable()->comment('负责销售');
            $table->text('remark')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->index(['store_id', 'status']);
            $table->index('owner_id');
            $table->index('phone');
        });

        // 线索跟进记录
        Schema::create('lead_follows', function (Blueprint $table) {
            $table->id();
            $table->foreignId('lead_id')->constrained()->onDelete('cascade');
            $table->unsignedBigInteger('user_id');
            $table->tinyInteger('type')->nullable()->comment('1电话 2微信 3到店 4其他');
            $table->text('content')->nullable();
            $table->timestamp('next_follow_at')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->index('lead_id');
        });

        // 客户
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->unsignedBigInteger('lead_id')->nullable();
            $table->string('name', 50);
            $table->string('phone', 20);
            $table->string('id_card', 20)->nullable();
            $table->string('wechat', 50)->nullable();
            $table->date('birthday')->nullable();
            $table->date('expected_date')->nullable();
            $table->date('actual_date')->nullable();
            $table->tinyInteger('baby_count')->default(1);
            $table->json('tags')->nullable();
            $table->tinyInteger('status')->default(1)->comment('1潜在 2签约 3在住 4离店 5无效');
            $table->unsignedBigInteger('owner_id')->nullable();
            $table->text('remark')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['store_id', 'status']);
            $table->index('phone');
        });

        // 客户家属
        Schema::create('customer_families', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->constrained()->onDelete('cascade');
            $table->string('name', 50)->nullable();
            $table->string('phone', 20)->nullable();
            $table->string('relation', 20)->nullable();
            $table->tinyInteger('is_emergency')->default(0);
            $table->timestamps();
            $table->index('customer_id');
        });

        // 合同
        Schema::create('contracts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->string('contract_no', 50)->unique();
            $table->string('package_name', 100)->nullable();
            $table->decimal('total_amount', 12, 2);
            $table->decimal('discount_amount', 12, 2)->default(0);
            $table->decimal('actual_amount', 12, 2);
            $table->integer('days')->nullable();
            $table->date('check_in_date')->nullable();
            $table->date('check_out_date')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待审 1通过 2无效 3已退');
            $table->unsignedBigInteger('audit_user_id')->nullable();
            $table->timestamp('audit_at')->nullable();
            $table->string('audit_remark', 255)->nullable();
            $table->text('remark')->nullable();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->index('store_id');
            $table->index('customer_id');
        });

        // 问卷模板
        Schema::create('questionnaire_templates', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->nullable();
            $table->string('title', 100);
            $table->text('description')->nullable();
            $table->json('questions')->nullable();
            $table->tinyInteger('type')->nullable()->comment('1入住前 2入住中 3离店 4回访');
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        // 问卷回收
        Schema::create('questionnaire_answers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('questionnaire_template_id')->constrained('questionnaire_templates')->onDelete('cascade');
            $table->foreignId('customer_id')->constrained();
            $table->json('answers')->nullable();
            $table->decimal('score', 5, 2)->nullable();
            $table->tinyInteger('status')->default(0)->comment('0未完成 1已提交');
            $table->timestamps();
        });

        // 投诉
        Schema::create('complaints', function (Blueprint $table) {
            $table->id();
            $table->foreignId('store_id')->constrained();
            $table->foreignId('customer_id')->constrained();
            $table->tinyInteger('type')->nullable()->comment('1服务 2膳食 3环境 4其他');
            $table->text('content');
            $table->json('images')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0待处理 1处理中 2已解决 3已关闭');
            $table->unsignedBigInteger('handler_id')->nullable();
            $table->text('handle_result')->nullable();
            $table->timestamp('handle_at')->nullable();
            $table->timestamps();
            $table->index(['store_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('complaints');
        Schema::dropIfExists('questionnaire_answers');
        Schema::dropIfExists('questionnaire_templates');
        Schema::dropIfExists('contracts');
        Schema::dropIfExists('customer_families');
        Schema::dropIfExists('customers');
        Schema::dropIfExists('lead_follows');
        Schema::dropIfExists('leads');
        Schema::dropIfExists('channels');
    }
};
