<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Every outbound Hubtel Checkout request gets a row here BEFORE the API
 * call is made (client_reference generated first), so a webhook that
 * arrives referencing an unrecognized reference is obviously suspect
 * rather than ambiguous, and so a failed/never-called-back checkout is
 * still visible for support/debugging — "did the guest actually pay"
 * shouldn't depend on grepping logs.
 *
 * `purpose` distinguishes a folio balance payment from a booking deposit
 * — same Hubtel Checkout mechanism, different follow-up action on
 * success (see PaymentWebhookController).
 */
class CreatePaymentAttemptsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'purpose' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'folio_payment, booking_deposit',
            ],
            'folio_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'booking_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'client_reference' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
            ],
            'checkout_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
            ],
            'amount' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'pending',
                'comment'    => 'pending, success, failed',
            ],
            'provider_payment_type' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'null'       => true,
                'comment'    => 'e.g. mobilemoney, card — from Hubtel PaymentDetails.PaymentType',
            ],
            '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('folio_id');
        $this->forge->addKey('booking_id');
        $this->forge->addKey('status');
        $this->forge->addForeignKey('folio_id', 'folios', 'id', '', 'SET NULL');
        $this->forge->addForeignKey('booking_id', 'bookings', 'id', '', 'SET NULL');
        $this->forge->createTable('payment_attempts');
    }

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