<?php
/**
 * Definisi rute aplikasi SIPIUTANG.
 * Setiap rute mencantumkan izin (perm) yang diperiksa otomatis oleh router.
 */

use App\Core\Acl;
use App\Core\Auth;
use App\Core\Response;
use App\Core\Request;
use App\Core\Router;
use App\Core\Url;

$r = new Router();

// Middleware global: memaksa login & penggantian kata sandi awal
$r->globalMiddleware(function (array $route, array $params) {
    $public = ['/masuk', '/keluar', '/lupa-sandi'];
    $uri = Request::uri();
    if (in_array($uri, $public, true)) return;

    if (!Auth::check()) {
        if (Request::isAjax()) Response::json(['sukses' => false, 'pesan' => 'Sesi berakhir. Silakan masuk kembali.', 'redirect' => Url::to('/masuk')], 401);
        \App\Core\Session::set('_intended', $uri);
        Response::redirect('/masuk');
    }
    if (Auth::mustChangePassword() && $uri !== '/ganti-sandi') {
        Response::redirect('/ganti-sandi');
    }
});

// ----------------------------------------------------------------- AUTENTIKASI
$r->get('/masuk',        'AuthController@showLogin');
$r->post('/masuk',       'AuthController@login');
$r->any('/keluar',       'AuthController@logout');
$r->get('/ganti-sandi',  'AuthController@showChangePassword');
$r->post('/ganti-sandi', 'AuthController@changePassword');
$r->get('/profil',       'AuthController@profile');
$r->post('/profil',      'AuthController@updateProfile');

// ----------------------------------------------------------------- DASHBOARD
$r->get('/',            'DashboardController@index',  ['perm' => 'dashboard.view']);
$r->get('/dashboard',   'DashboardController@index',  ['perm' => 'dashboard.view']);
$r->get('/dashboard/data', 'DashboardController@data', ['perm' => 'dashboard.view']);
$r->get('/dashboard/drill', 'DashboardController@drill', ['perm' => 'dashboard.view']);
$r->get('/dashboard/ekspor', 'DashboardController@export', ['perm' => 'dashboard.export']);

// ----------------------------------------------------------------- PIUTANG
$r->get('/piutang',            'PiutangController@index',   ['perm' => 'piutang.view']);
$r->get('/piutang/ekspor',     'PiutangController@export',  ['perm' => 'piutang.export']);
$r->get('/aging',              'AgingController@index',     ['perm' => 'aging.view']);
$r->get('/aging/ekspor',       'AgingController@export',    ['perm' => 'aging.export']);
$r->get('/aging/detail',       'AgingController@detail',    ['perm' => 'aging.view']);
$r->get('/kartu-piutang',      'PiutangController@cards',   ['perm' => 'kartu_piutang.view']);
$r->get('/kartu-piutang/{id}', 'PiutangController@card',    ['perm' => 'kartu_piutang.view']);
$r->get('/kartu-piutang/{id}/ekspor', 'PiutangController@cardExport', ['perm' => 'kartu_piutang.export']);
$r->get('/forecast',           'PiutangController@forecast',['perm' => 'forecast.view']);
$r->get('/forecast/ekspor',    'PiutangController@forecastExport', ['perm' => 'forecast.export']);

// ----------------------------------------------------------------- ACP
$r->get('/acp',            'AcpController@index',   ['perm' => 'acp.view']);
$r->get('/acp/ekspor',     'AcpController@export',  ['perm' => 'acp.export']);
$r->get('/acp/target',     'AcpController@targets', ['perm' => 'acp.view']);
$r->post('/acp/target',    'AcpController@saveTarget', ['perm' => 'acp.edit']);
$r->post('/acp/target/hapus', 'AcpController@deleteTarget', ['perm' => 'acp.edit']);

// ----------------------------------------------------------------- KUALITAS DATA
$r->get('/kualitas-data',        'DataQualityController@index',  ['perm' => 'kualitas_data.view']);
$r->get('/kualitas-data/detail', 'DataQualityController@detail', ['perm' => 'kualitas_data.view']);
$r->get('/kualitas-data/ekspor', 'DataQualityController@export', ['perm' => 'kualitas_data.export']);
$r->post('/kualitas-data/perbaiki', 'DataQualityController@autoFix', ['perm' => 'kualitas_data.view']);

// ----------------------------------------------------------------- INVOICE
$r->get('/invoice',              'InvoiceController@index',   ['perm' => 'invoice.view']);
$r->get('/invoice/tambah',       'InvoiceController@create',  ['perm' => 'invoice.create']);
$r->post('/invoice/tambah',      'InvoiceController@store',   ['perm' => 'invoice.create']);
$r->get('/invoice/ekspor',       'InvoiceController@export',  ['perm' => 'invoice.export']);
$r->get('/invoice/cari',         'InvoiceController@search',  ['perm' => 'invoice.view']);
$r->get('/invoice/{id}',         'InvoiceController@show',    ['perm' => 'invoice.view']);
$r->get('/invoice/{id}/ubah',    'InvoiceController@edit',    ['perm' => 'invoice.edit']);
$r->post('/invoice/{id}/ubah',   'InvoiceController@update',  ['perm' => 'invoice.edit']);
$r->post('/invoice/{id}/hapus',  'InvoiceController@destroy', ['perm' => 'invoice.delete']);
$r->post('/invoice/{id}/batalkan','InvoiceController@cancel',  ['perm' => 'invoice.edit']);
$r->post('/invoice/{id}/hitung-ulang', 'InvoiceController@recalc', ['perm' => 'invoice.edit']);
$r->get('/invoice/{id}/cetak',   'InvoiceController@print',   ['perm' => 'invoice.print']);
$r->post('/invoice/massal',      'InvoiceController@bulk',    ['perm' => 'invoice.edit']);

// ----------------------------------------------------------------- PEMBAYARAN
$r->get('/pembayaran',            'PaymentController@index',  ['perm' => 'pembayaran.view']);
$r->get('/pembayaran/tambah',     'PaymentController@create', ['perm' => 'pembayaran.create']);
$r->post('/pembayaran/tambah',    'PaymentController@store',  ['perm' => 'pembayaran.create']);
$r->get('/pembayaran/saran-alokasi', 'PaymentController@suggest', ['perm' => 'pembayaran.create']);
$r->get('/pembayaran/ekspor',     'PaymentController@export', ['perm' => 'pembayaran.export']);
$r->get('/pembayaran/{id}',       'PaymentController@show',   ['perm' => 'pembayaran.view']);
$r->get('/pembayaran/{id}/ubah',  'PaymentController@edit',   ['perm' => 'pembayaran.edit']);
$r->post('/pembayaran/{id}/ubah', 'PaymentController@update', ['perm' => 'pembayaran.edit']);
$r->post('/pembayaran/{id}/balik','PaymentController@reverse',['perm' => 'pembayaran.edit']);
$r->post('/pembayaran/{id}/hapus','PaymentController@destroy',['perm' => 'pembayaran.delete']);
$r->post('/pembayaran/{id}/setujui','PaymentController@approve',['perm' => 'pembayaran.approve']);
$r->get('/pembayaran/{id}/cetak', 'PaymentController@print',  ['perm' => 'pembayaran.print']);

// ----------------------------------------------------------------- PENAGIHAN
$r->get('/penagihan',              'CollectionController@index',   ['perm' => 'penagihan.view']);
$r->get('/penagihan/riwayat',      'CollectionController@history', ['perm' => 'penagihan.view']);
$r->get('/penagihan/janji-bayar',  'CollectionController@promises',['perm' => 'penagihan.view']);
$r->get('/penagihan/tambah',       'CollectionController@create',  ['perm' => 'penagihan.create']);
$r->post('/penagihan/tambah',      'CollectionController@store',   ['perm' => 'penagihan.create']);
$r->get('/penagihan/ekspor',       'CollectionController@export',  ['perm' => 'penagihan.export']);
$r->post('/penagihan/evaluasi-janji', 'CollectionController@evaluate', ['perm' => 'penagihan.edit']);
$r->post('/penagihan/tugaskan',    'CollectionController@assign',  ['perm' => 'penagihan.edit']);
$r->get('/penagihan/{id}/ubah',    'CollectionController@edit',    ['perm' => 'penagihan.edit']);
$r->post('/penagihan/{id}/ubah',   'CollectionController@update',  ['perm' => 'penagihan.edit']);
$r->post('/penagihan/{id}/hapus',  'CollectionController@destroy', ['perm' => 'penagihan.delete']);

// ----------------------------------------------------------------- REVISI (RF)
$r->get('/revisi',              'RevisionController@index',  ['perm' => 'revisi.view']);
$r->get('/revisi/tambah',       'RevisionController@create', ['perm' => 'revisi.create']);
$r->post('/revisi/tambah',      'RevisionController@store',  ['perm' => 'revisi.create']);
$r->get('/revisi/ekspor',       'RevisionController@export', ['perm' => 'revisi.export']);
$r->post('/revisi/cocokkan-ulang','RevisionController@rematch',['perm' => 'revisi.edit']);
$r->get('/revisi/{id}',         'RevisionController@show',   ['perm' => 'revisi.view']);
$r->get('/revisi/{id}/ubah',    'RevisionController@edit',   ['perm' => 'revisi.edit']);
$r->post('/revisi/{id}/ubah',   'RevisionController@update', ['perm' => 'revisi.edit']);
$r->post('/revisi/{id}/setujui','RevisionController@approve',['perm' => 'revisi.approve']);
$r->post('/revisi/{id}/tolak',  'RevisionController@reject', ['perm' => 'revisi.approve']);
$r->post('/revisi/{id}/hapus',  'RevisionController@destroy',['perm' => 'revisi.delete']);

// ----------------------------------------------------------------- NOTA KREDIT
$r->get('/nota-kredit',            'CreditNoteController@index',  ['perm' => 'nota_kredit.view']);
$r->get('/nota-kredit/tambah',     'CreditNoteController@create', ['perm' => 'nota_kredit.create']);
$r->post('/nota-kredit/tambah',    'CreditNoteController@store',  ['perm' => 'nota_kredit.create']);
$r->get('/nota-kredit/ekspor',     'CreditNoteController@export', ['perm' => 'nota_kredit.export']);
$r->post('/nota-kredit/{id}/setujui','CreditNoteController@approve',['perm' => 'nota_kredit.approve']);
$r->post('/nota-kredit/{id}/tolak','CreditNoteController@reject', ['perm' => 'nota_kredit.approve']);
$r->post('/nota-kredit/{id}/hapus','CreditNoteController@destroy',['perm' => 'nota_kredit.delete']);

// ----------------------------------------------------------------- IMPORT
$r->get('/import',                'ImportController@index',    ['perm' => 'import.view']);
$r->get('/import/wizard',         'ImportController@wizard',   ['perm' => 'import.create']);
$r->post('/import/unggah',        'ImportController@upload',   ['perm' => 'import.create']);
$r->post('/import/pratinjau',     'ImportController@preview',  ['perm' => 'import.create']);
$r->post('/import/validasi',      'ImportController@validateData', ['perm' => 'import.create']);
$r->post('/import/jalankan',      'ImportController@run',      ['perm' => 'import.import']);
$r->get('/import/{id}',           'ImportController@show',     ['perm' => 'import.view']);
$r->get('/import/{id}/baris-gagal','ImportController@rejected', ['perm' => 'import.view']);
$r->post('/import/{id}/rollback', 'ImportController@rollback', ['perm' => 'import.delete']);

// ----------------------------------------------------------------- LAPORAN
$r->get('/laporan',               'ReportController@index',   ['perm' => 'laporan.view']);
$r->get('/laporan/{kode}',        'ReportController@show',    ['perm' => 'laporan.view']);
$r->get('/laporan/{kode}/ekspor', 'ReportController@export',  ['perm' => 'laporan.export']);

// ----------------------------------------------------------------- PEMBUAT LAPORAN
$r->get('/pembuat-laporan',            'ReportBuilderController@index',    ['perm' => 'report_builder.view']);
$r->get('/pembuat-laporan/baru',       'ReportBuilderController@builder',  ['perm' => 'report_builder.view']);
$r->get('/pembuat-laporan/field',      'ReportBuilderController@fields',   ['perm' => 'report_builder.view']);
$r->post('/pembuat-laporan/jalankan',  'ReportBuilderController@run',      ['perm' => 'report_builder.view']);
$r->post('/pembuat-laporan/simpan',    'ReportBuilderController@save',     ['perm' => 'report_builder.create']);
$r->post('/pembuat-laporan/{id}/duplikat','ReportBuilderController@duplicate',['perm' => 'report_builder.create']);
$r->post('/pembuat-laporan/{id}/bagikan','ReportBuilderController@share',  ['perm' => 'report_builder.edit']);
$r->post('/pembuat-laporan/{id}/hapus','ReportBuilderController@destroy',  ['perm' => 'report_builder.delete']);
$r->get('/pembuat-laporan/{id}',       'ReportBuilderController@builder',  ['perm' => 'report_builder.view']);
$r->post('/pembuat-laporan/ekspor',    'ReportBuilderController@export',   ['perm' => 'report_builder.export']);

// ----------------------------------------------------------------- DATA MASTER
$r->get('/master',                  'MasterController@index',   ['perm' => 'master.view']);
$r->get('/master/{entity}',         'MasterController@listing', ['perm' => 'master.view']);
$r->get('/master/{entity}/tambah',  'MasterController@create',  ['perm' => 'master.create']);
$r->post('/master/{entity}/tambah', 'MasterController@store',   ['perm' => 'master.create']);
$r->get('/master/{entity}/{id}/ubah','MasterController@edit',   ['perm' => 'master.edit']);
$r->post('/master/{entity}/{id}/ubah','MasterController@update', ['perm' => 'master.edit']);
$r->post('/master/{entity}/{id}/hapus','MasterController@destroy',['perm' => 'master.delete']);
$r->get('/master/{entity}/ekspor',  'MasterController@export',  ['perm' => 'master.export']);

// ----------------------------------------------------------------- PERSETUJUAN
$r->get('/persetujuan',            'ApprovalController@index',  ['perm' => 'persetujuan.view']);
$r->get('/persetujuan/{id}',       'ApprovalController@show',   ['perm' => 'persetujuan.view']);
$r->post('/persetujuan/{id}/putuskan','ApprovalController@decide',['perm' => 'persetujuan.approve']);
$r->get('/persetujuan-ekspor',     'ApprovalController@export', ['perm' => 'persetujuan.export']);

// ----------------------------------------------------------------- PENGGUNA
$r->get('/pengguna',               'UserController@index',   ['perm' => 'pengguna.view']);
$r->get('/pengguna/tambah',        'UserController@create',  ['perm' => 'pengguna.create']);
$r->post('/pengguna/tambah',       'UserController@store',   ['perm' => 'pengguna.create']);
$r->get('/pengguna/peran',         'UserController@roles',   ['perm' => 'pengguna.view']);
$r->post('/pengguna/peran/simpan', 'UserController@saveRolePermissions', ['perm' => 'pengguna.edit']);
$r->get('/pengguna/{id}/ubah',     'UserController@edit',    ['perm' => 'pengguna.edit']);
$r->post('/pengguna/{id}/ubah',    'UserController@update',  ['perm' => 'pengguna.edit']);
$r->post('/pengguna/{id}/hapus',   'UserController@destroy', ['perm' => 'pengguna.delete']);
$r->post('/pengguna/{id}/reset-sandi','UserController@resetPassword', ['perm' => 'pengguna.edit']);
$r->post('/pengguna/{id}/aktifkan','UserController@toggleActive', ['perm' => 'pengguna.edit']);

// ----------------------------------------------------------------- AUDIT
$r->get('/log-aktivitas',        'AuditController@index',  ['perm' => 'audit.view']);
$r->get('/log-aktivitas/ekspor', 'AuditController@export', ['perm' => 'audit.export']);
$r->get('/log-aktivitas/{id}',   'AuditController@show',   ['perm' => 'audit.view']);

// ----------------------------------------------------------------- PENGATURAN
$r->get('/pengaturan',                 'SettingController@index',      ['perm' => 'pengaturan.view']);
$r->post('/pengaturan/simpan',         'SettingController@save',       ['perm' => 'pengaturan.edit']);
$r->get('/pengaturan/aging',           'SettingController@aging',      ['perm' => 'pengaturan.view']);
$r->post('/pengaturan/aging/simpan',   'SettingController@saveAging',  ['perm' => 'pengaturan.edit']);
$r->post('/pengaturan/aging/hapus',    'SettingController@deleteAging',['perm' => 'pengaturan.edit']);
$r->post('/pengaturan/hitung-ulang',   'SettingController@recalculate',['perm' => 'pengaturan.edit']);

// ----------------------------------------------------------------- PENCADANGAN
$r->get('/pencadangan',           'BackupController@index',   ['perm' => 'backup.view']);
$r->post('/pencadangan/buat',     'BackupController@create',  ['perm' => 'backup.create']);
$r->get('/pencadangan/unduh',     'BackupController@download',['perm' => 'backup.view']);
$r->post('/pencadangan/hapus',    'BackupController@destroy', ['perm' => 'backup.delete']);
$r->post('/pencadangan/pulihkan', 'BackupController@restore', ['perm' => 'backup.create']);

// ----------------------------------------------------------------- LAMPIRAN
$r->post('/lampiran/unggah',      'AttachmentController@upload',  ['perm' => 'lampiran.create']);
$r->get('/lampiran/{id}',         'AttachmentController@download',['perm' => 'lampiran.view']);
$r->post('/lampiran/{id}/hapus',  'AttachmentController@destroy', ['perm' => 'lampiran.delete']);

return $r;
