<?php

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

/**
 * PRC-697 — Pro-rata (genel / ilk sipariş anchor) faturalama.
 *
 * Ürün ve ürün-grubu bazında sabit yenileme günü (billing anchor day, ayın 1-31'i)
 * + prorate aç/kapa bayrağı. Grup-seviyesi ayar, o gruptaki ürünlere fallback'tir;
 * ürün-seviyesi override eder. Her ikisi de nullable → geriye uyumlu (varsayılan yok
 * = prorate uygulanmaz; global setting `proration_enabled` de kapalı gelir).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('products', function (Blueprint $table): void {
            // NULL = grup fallback / global setting'e düş; true/false = ürün override.
            $table->boolean('proration_enabled')->nullable()->after('upgrades_enabled');
            // 1-31 arası sabit yenileme günü; NULL = anchor yok (prorate uygulanmaz).
            $table->unsignedTinyInteger('billing_anchor_day')->nullable()->after('proration_enabled');
        });

        Schema::table('product_groups', function (Blueprint $table): void {
            $table->boolean('proration_enabled')->nullable()->after('id');
            $table->unsignedTinyInteger('billing_anchor_day')->nullable()->after('proration_enabled');
        });
    }

    public function down(): void
    {
        Schema::table('products', function (Blueprint $table): void {
            $table->dropColumn(['proration_enabled', 'billing_anchor_day']);
        });

        Schema::table('product_groups', function (Blueprint $table): void {
            $table->dropColumn(['proration_enabled', 'billing_anchor_day']);
        });
    }
};
