<?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('suppliers', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_id');
            
            // Kişi veya Şirket
            $table->enum('type', ['individual', 'corporate'])->default('individual');
            
            // Tedarikçi tipi: oteller, taşıma firması, restoran/lokanta/mekan, sigorta şirketi, vize danışmanı ve havayolu firması
            $table->enum('supplier_type', ['hotel', 'transportation', 'restaurant', 'insurance', 'visa', 'airline']);

            // Kişisel Bilgiler
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();

            // Kurumsal Bilgiler
            $table->string('company_name')->nullable();
            $table->string('company_tax_number')->nullable();
            $table->string('company_tax_office')->nullable();

            // Sorumlu Kişi Bilgileri
            $table->string('responsible_person_name')->nullable();
            $table->string('responsible_person_phone', 30)->nullable();
            $table->string('responsible_person_email')->nullable();

            // Fatura Bilgileri
            $table->text('billing_address')->nullable();
            $table->string('billing_city')->nullable();
            $table->string('billing_district')->nullable();
            $table->string('billing_tax_number', 50)->nullable();
            $table->string('billing_tax_office')->nullable();
            $table->string('billing_email')->nullable();

            // İletişim Bilgileri
            $table->json('emails')->nullable();
            $table->json('phones')->nullable();
            $table->string('phone', 30)->nullable(); // Birincil telefon
            $table->text('address')->nullable();
            $table->string('city')->nullable();
            $table->string('district')->nullable();
            $table->string('country', 2)->default('TR');
            $table->string('postal_code', 10)->nullable();

            $table->text('notes')->nullable();
            $table->enum('status', ['active', 'inactive', 'blocked', 'potential'])->default('active');

            // Audit
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign Keys
            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // Indexes
            $table->index(['agency_id', 'status']);
            $table->index(['agency_id', 'supplier_type']);
        });
    }

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