<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Control DB. Payment ledger for the platform COLLECTING from hotels
 * (subscription payments via Hubtel Checkout) — the mirror image of a
 * tenant's `payment_attempts` table (Phase 3, guest payments to the
 * hotel), but a distinct Hubtel merchant context: the platform's own
 * Hubtel account (see CreatePlatformSettingsTable), not any hotel's.
 * `client_reference` is the idempotency key PaymentWebhookController
 * matches on, same pattern as payment_attempts.
 */
class CreateTransactionsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'tenant_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'invoice_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'amount' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
            ],
            'type' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'charge',
                'comment'    => 'charge|refund',
            ],
            'method' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'null'       => true,
                'comment'    => 'hubtel_momo|hubtel_card|manual|bank_transfer',
            ],
            'reference' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
                'comment'    => "Hubtel's own transaction id, from the webhook payload.",
            ],
            'client_reference' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
                'comment'    => 'Our idempotency key, generated before calling Hubtel Checkout — webhook matches back on this.',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'pending',
                'comment'    => 'pending|success|failed',
            ],
            'raw_response' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('client_reference');
        $this->forge->addKey('tenant_id');
        $this->forge->addForeignKey('tenant_id', 'tenants', 'id', '', 'CASCADE');
        $this->forge->addForeignKey('invoice_id', 'invoices', 'id', '', 'SET NULL');
        $this->forge->createTable('transactions');
    }

    public function down()
    {
        $this->forge->dropTable('transactions', true);
    }
}
