<?php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

use App\Models\TahunAjaran;
use App\Models\Kelas;
use App\Models\Siswa;
use App\Models\TarifPembayaran;
use App\Models\Tagihan;
use App\Models\TransaksiPembayaran;
use App\Models\DetailPembayaran;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

// Helper to output
function out($msg) {
    echo $msg . "\n";
}

// Ensure a user exists for auth
$user = User::firstOrCreate(['email' => 'test@example.com'], ['name' => 'Test User', 'password' => bcrypt('secret')]);
Auth::loginUsingId($user->id);

// Clean up previous test data
TahunAjaran::where('tahun', 'like', '2025%')->orWhere('tahun', 'like', '2026%')->delete();
Kelas::where('nama_kelas', 'like', 'X%')->delete();
Siswa::where('nis', 'like', '999%')->delete();
TarifPembayaran::where('nama', 'like', 'Test%')->delete();
Tagihan::where('siswa_id', $user->id)->delete();
TransaksiPembayaran::where('siswa_id', $user->id)->delete();

// 1. Create TA 2025/2026 active
$ta1 = TahunAjaran::create(['tahun' => '2025/2026', 'status' => 'aktif']);
// 2. Create class
$kelas1 = Kelas::create(['nama_kelas' => 'X IPA 1', 'jenjang' => 'X', 'tahun_ajaran_id' => $ta1->id]);
// 3. Create student
$siswa = Siswa::create(['nis' => '999001', 'nama_lengkap' => 'Student Snapshot', 'kategori' => 'regular', 'status' => 'aktif', 'latest_siswa_kelas_id' => null]);
// Attach class via pivot (assume relationship exists)
$siswa->kelas()->attach($kelas1->id, ['tahun_ajaran_id' => $ta1->id]);
// Update latestSiswaKelas relationship if needed (simplified)
$siswa->latest_siswa_kelas_id = $kelas1->id;
$siswa->save();

// 4. Create tarif SPP 100k
$tarifSPP1 = TarifPembayaran::create(['nama' => 'Test SPP 100k', 'jenis' => 'SPP', 'jenjang' => 'X', 'nominal' => 100000, 'status' => 'aktif', 'tahun_ajaran_id' => $ta1->id]);

// 5. Create tagihan snapshot for August 2025
$tagihan1 = Tagihan::create([
    'siswa_id' => $siswa->id,
    'tarif_pembayaran_id' => $tarifSPP1->id,
    'kelas_id' => $kelas1->id,
    'nama_kelas_snapshot' => $kelas1->nama_kelas,
    'nama_siswa_snapshot' => $siswa->nama_lengkap,
    'tahun_ajaran_id' => $ta1->id,
    'tahun_ajaran_snapshot' => $ta1->id,
    'tarif_lama' => $tarifSPP1->nominal,
    'bulan' => 8,
    'tahun' => 2025,
    'jumlah_tagihan' => $tarifSPP1->nominal,
    'jumlah_terbayar' => 0,
    'status' => 'belum_bayar',
]);

out('Created Tagihan1: tarif_lama=' . $tagihan1->tarif_lama);

// 6. Create transaction paying full SPP
$trans = TransaksiPembayaran::create([
    'kode_transaksi' => TransaksiPembayaran::generateKodeTransaksi(),
    'siswa_id' => $siswa->id,
    'user_id' => $user->id,
    'tahun_ajaran_id' => $ta1->id,
    'nama_kelas_snapshot' => $kelas1->nama_kelas,
    'tahun_ajaran_snapshot' => $ta1->id,
    'tarif_snapshot' => $tarifSPP1->nominal,
    'tanggal_bayar' => now()->toDateString(),
    'total_bayar' => $tarifSPP1->nominal,
    'metode_pembayaran' => 'tunai',
    'keterangan' => 'Test payment',
]);
DetailPembayaran::create(['transaksi_pembayaran_id' => $trans->id, 'tagihan_id' => $tagihan1->id, 'jumlah_bayar' => $tarifSPP1->nominal]);
$tagihan1->jumlah_terbayar += $tarifSPP1->nominal;
$tagihan1->updateStatus();
out('Transaction created, Tagihan1 status=' . $tagihan1->status);

// 7. Switch active year to 2026/2027 with new tarif
$ta1->update(['status' => 'nonaktif']);
$ta2 = TahunAjaran::create(['tahun' => '2026/2027', 'status' => 'aktif']);
$tarifSPP2 = TarifPembayaran::create(['nama' => 'Test SPP 150k', 'jenis' => 'SPP', 'jenjang' => 'X', 'nominal' => 150000, 'status' => 'aktif', 'tahun_ajaran_id' => $ta2->id]);

// 8. Create new tagihan for August 2026 (new year)
$tagihan2 = Tagihan::create([
    'siswa_id' => $siswa->id,
    'tarif_pembayaran_id' => $tarifSPP2->id,
    'kelas_id' => $kelas1->id,
    'nama_kelas_snapshot' => $kelas1->nama_kelas,
    'nama_siswa_snapshot' => $siswa->nama_lengkap,
    'tahun_ajaran_id' => $ta2->id,
    'tahun_ajaran_snapshot' => $ta2->id,
    'tarif_lama' => $tarifSPP2->nominal,
    'bulan' => 8,
    'tahun' => 2026,
    'jumlah_tagihan' => $tarifSPP2->nominal,
    'jumlah_terbayar' => 0,
    'status' => 'belum_bayar',
]);
out('Created Tagihan2: tarif_lama=' . $tagihan2->tarif_lama);

// Verify old tagihan still has old tarif
$out1 = Tagihan::find($tagihan1->id);
$out2 = Tagihan::find($tagihan2->id);
out('Verification: Tagihan1 tarif_lama=' . $out1->tarif_lama . ', Tagihan2 tarif_lama=' . $out2->tarif_lama);

?>
