<?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('detail_penjualan', function (Blueprint $table) {
            $table->id('detail_id');

            // relasi ke tabel penjualan
            $table->foreignId('penjualan_id')
                  ->constrained('penjualan', 'penjualan_id')
                  ->cascadeOnUpdate()
                  ->cascadeOnDelete();

            // relasi ke produk
            $table->foreignId('produk_id')
                  ->constrained('product', 'produk_id')
                  ->cascadeOnUpdate()
                  ->restrictOnDelete();

            // jumlah produk dibeli
            $table->integer('jumlah'); // ganti dari jumlah_produk → jumlah

            // harga per produk pada saat transaksi
            $table->decimal('harga_satuan', 10, 2);

            // total harga produk * jumlah
            $table->decimal('subtotal', 10, 2);

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

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