<?php

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

/**
 * PRC-370 / [5/19] M2 — Cumulative invoice generation.
 *
 * Adds `period_start` + `period_end` to `invoice_items` so each line
 * records which billing cycle it covers. Needed for:
 *   1. Cumulative line-addition: appending a new cycle's line to an
 *      unpaid invoice (vs creating a fresh invoice).
 *   2. Payment-time `next_due_date` push: the service's next due jumps
 *      to the latest paid line's `period_end`.
 *
 * Both are nullable — legacy lines + ad-hoc fee lines have no cycle.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->date('period_start')->nullable()->after('period_type');
            $table->date('period_end')->nullable()->after('period_start');
        });
    }

    public function down(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->dropColumn(['period_start', 'period_end']);
        });
    }
};
