<?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::table('sales_daily', function (Blueprint $table) {
            // Detail produk dalam JSON
            $table->json('detail_produk')->nullable()->after('tipe_pembelian');

            // Pengiriman & finance
            $table->decimal('biaya_kirim', 12, 0)->default(0)->after('nominal_gmv');
            $table->decimal('grand_total', 12, 0)->default(0)->after('biaya_kirim');

            // Data customer
            $table->string('no_telp_pemesan')->nullable()->after('nama_pemesan');
            $table->string('nama_penerima')->nullable()->after('no_telp_pemesan');
            $table->string('no_telp_penerima')->nullable()->after('nama_penerima');
            $table->text('alamat')->nullable()->after('no_telp_penerima');
            $table->string('kelurahan')->nullable()->after('alamat');
            $table->string('kecamatan')->nullable()->after('kelurahan');
            $table->string('kota')->nullable()->after('kecamatan');
            $table->string('provinsi')->nullable()->after('kota');
            $table->string('jasa_kirim')->nullable()->after('provinsi');
            $table->string('nomor_resi')->nullable()->after('jasa_kirim');
            $table->text('catatan')->nullable()->after('nomor_resi');
        });

        // Tabel master produk
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('kategori');        // TOPLES_REGULER, POUCH, dll
            $table->string('kode');            // O, W, CF, dll
            $table->string('nama');            // nama lengkap
            $table->decimal('harga', 12, 0)->default(0);
            $table->integer('urutan')->default(0);
            $table->boolean('aktif')->default(true);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::table('sales_daily', function (Blueprint $table) {
            $table->dropColumn([
                'detail_produk',
                'biaya_kirim', 'grand_total',
                'no_telp_pemesan', 'nama_penerima', 'no_telp_penerima',
                'alamat', 'kelurahan', 'kecamatan', 'kota', 'provinsi',
                'jasa_kirim', 'nomor_resi', 'catatan',
            ]);
        });
        Schema::dropIfExists('products');
    }
};
