<?php

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

class CreateIdentityVerificationLogsTable extends Migration
{
    /**
     * Run the migrations.
     * Tạo bảng lưu lịch sử xác minh danh tính
     *
     * @return void
     */
    public function up()
    {
        Schema::create('identity_verification_logs', function (Blueprint $table) {
            $table->id();

            // User reference
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            // Order reference (optional - for tracking which order triggered verification)
            $table->unsignedBigInteger('order_id')->nullable();
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('set null');

            // Action type
            $table->enum('action', [
                'submitted',           // User nộp giấy tờ
                'approved',            // Admin duyệt
                'rejected',            // Admin từ chối
                'resubmitted',         // User nộp lại sau khi bị từ chối
                'auto_cancelled',      // Hệ thống tự động hủy do quá hạn
                'document_updated',    // User cập nhật giấy tờ
            ])->comment('Loại hành động');

            // Status before and after
            $table->enum('status_before', ['not_submitted', 'pending', 'approved', 'rejected'])->nullable()
                  ->comment('Trạng thái trước khi thực hiện');
            $table->enum('status_after', ['not_submitted', 'pending', 'approved', 'rejected'])
                  ->comment('Trạng thái sau khi thực hiện');

            // Document type used
            $table->enum('document_type', ['driver_license', 'my_number_card'])->nullable()
                  ->comment('Loại giấy tờ sử dụng');

            // Document images at the time of action (for audit trail)
            $table->string('driver_license_front', 500)->nullable()
                  ->comment('Ảnh mặt trước bằng lái xe tại thời điểm');
            $table->string('driver_license_back', 500)->nullable()
                  ->comment('Ảnh mặt sau bằng lái xe tại thời điểm');
            $table->string('my_number_card_front', 500)->nullable()
                  ->comment('Ảnh mặt trước thẻ My Number tại thời điểm');

            // Admin who performed the action (for approve/reject)
            $table->unsignedBigInteger('admin_id')->nullable();
            $table->foreign('admin_id')->references('id')->on('users')->onDelete('set null');
            $table->string('admin_name', 255)->nullable()
                  ->comment('Tên admin thực hiện (lưu để audit)');

            // Rejection details
            $table->text('rejection_reason')->nullable()
                  ->comment('Lý do từ chối');
            $table->unsignedTinyInteger('rejection_count')->default(0)
                  ->comment('Số lần bị từ chối tại thời điểm');

            // Additional notes
            $table->text('notes')->nullable()
                  ->comment('Ghi chú thêm');

            // IP address for security audit
            $table->string('ip_address', 45)->nullable()
                  ->comment('Địa chỉ IP');
            $table->text('user_agent')->nullable()
                  ->comment('User agent');

            $table->timestamps();

            // Indexes
            $table->index('user_id');
            $table->index('order_id');
            $table->index('action');
            $table->index('created_at');
            $table->index(['user_id', 'action']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('identity_verification_logs');
    }
}
