<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('m_shipment_suppliers', function (Blueprint $table) {
            $table->id();
            $table->string('code', 20)->unique();
            $table->string('name', 255);
            $table->string('contact_person', 255)->nullable();
            $table->string('phone', 20)->nullable();
            $table->string('email', 255)->nullable();
            $table->text('address')->nullable();
            $table->json('contact_days')->nullable(); // ['monday', 'tuesday', etc.]
            $table->time('contact_time_start')->nullable();
            $table->time('contact_time_end')->nullable();
            $table->integer('lead_time_days')->default(1);
            $table->text('notes')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('delete_flag')->default(false);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->index(['delete_flag', 'is_active']);
            $table->index('code');
        });

        // Insert sample suppliers
        DB::table('m_shipment_suppliers')->insert([
            [
                'code' => 'SUP001',
                'name' => 'PT. Supplier Utama',
                'contact_person' => 'John Doe',
                'phone' => '021-1234567',
                'email' => 'john@supplier-utama.com',
                'address' => 'Jl. Raya No. 123, Jakarta',
                'contact_days' => json_encode(['monday', 'tuesday', 'wednesday', 'thursday', 'friday']),
                'contact_time_start' => '08:00',
                'contact_time_end' => '17:00',
                'lead_time_days' => 3,
                'notes' => 'Supplier utama untuk bahan baku',
                'is_active' => true,
                'delete_flag' => false,
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'code' => 'SUP002',
                'name' => 'CV. Supplier Alternatif',
                'contact_person' => 'Jane Smith',
                'phone' => '021-7654321',
                'email' => 'jane@supplier-alt.com',
                'address' => 'Jl. Alternatif No. 456, Jakarta',
                'contact_days' => json_encode(['monday', 'wednesday', 'friday']),
                'contact_time_start' => '09:00',
                'contact_time_end' => '16:00',
                'lead_time_days' => 5,
                'notes' => 'Supplier alternatif dengan harga kompetitif',
                'is_active' => true,
                'delete_flag' => false,
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'code' => 'SUP003',
                'name' => 'PT. Express Supplier',
                'contact_person' => 'Bob Wilson',
                'phone' => '021-9876543',
                'email' => 'bob@express-supplier.com',
                'address' => 'Jl. Express No. 789, Jakarta',
                'contact_days' => json_encode(['tuesday', 'thursday', 'saturday']),
                'contact_time_start' => '10:00',
                'contact_time_end' => '15:00',
                'lead_time_days' => 1,
                'notes' => 'Supplier express untuk kebutuhan mendesak',
                'is_active' => true,
                'delete_flag' => false,
                'created_at' => now(),
                'updated_at' => now()
            ]
        ]);
    }

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