<?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('corporate_vehicles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->index();

            // 기본 정보
            $table->string('plate_number', 20)->comment('차량번호');
            $table->string('model', 100)->comment('모델명');
            $table->string('vehicle_type', 20)->comment('종류: 승용차, 승합차, 화물차, SUV');
            $table->enum('ownership_type', ['corporate', 'rent', 'lease'])->default('corporate')->comment('구분: 법인, 렌트, 리스');
            $table->year('year')->nullable()->comment('연식');
            $table->string('driver', 50)->nullable()->comment('운전자');
            $table->enum('status', ['active', 'maintenance', 'disposed'])->default('active')->comment('상태');
            $table->unsignedInteger('mileage')->default(0)->comment('주행거리(km)');
            $table->text('memo')->nullable()->comment('메모');

            // 법인차량 전용
            $table->date('purchase_date')->nullable()->comment('취득일');
            $table->unsignedBigInteger('purchase_price')->default(0)->comment('취득가');

            // 렌트/리스 전용
            $table->date('contract_date')->nullable()->comment('계약일자');
            $table->string('rent_company', 100)->nullable()->comment('렌트/리스회사명');
            $table->string('rent_company_tel', 20)->nullable()->comment('회사 연락처');
            $table->string('rent_period', 20)->nullable()->comment('렌트/리스기간');
            $table->string('agreed_mileage', 20)->nullable()->comment('약정운행거리');
            $table->unsignedBigInteger('vehicle_price')->default(0)->comment('차량가격');
            $table->unsignedBigInteger('residual_value')->default(0)->comment('추정잔존가액');
            $table->unsignedBigInteger('deposit')->default(0)->comment('보증금');
            $table->unsignedBigInteger('monthly_rent')->default(0)->comment('월 렌트료 공급가액');
            $table->unsignedBigInteger('monthly_rent_tax')->default(0)->comment('월 렌트료 세액');
            $table->string('insurance_company', 100)->nullable()->comment('보험사명');
            $table->string('insurance_company_tel', 20)->nullable()->comment('보험사 연락처');

            $table->timestamps();
            $table->softDeletes();

            $table->index(['tenant_id', 'ownership_type']);
            $table->index(['tenant_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('corporate_vehicles');
    }
};
