<?php
/**
 * BookiFlex Payment Extension
 *
 * @package    BookiFlex
 * @subpackage Payment
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Payment\ORM\Schema;

use BookiFlex\Core\ORM\Schema\AbstractSchema;
use BookiFlex\Core\ORM\Schema\CustomerSchema;
use Cycle\Database\Schema\AbstractTable;

defined( 'ABSPATH' ) || exit;

/**
 * Payment table schema
 */
class PaymentSchema extends AbstractSchema
{
    public const TABLE_NAME                   = 'bflex_payments';

    public function define(): AbstractTable
    {
        $schema = $this->database->table(self::TABLE_NAME)->getSchema();
        $schema->column('id')->integer()->primary();
        $schema->column('client_id')->integer()->nullable()->defaultValue(null);
        $schema->column('total_amount')->integer();
        $schema->column('currency_code')->string(3)->nullable()->defaultValue(null);
        $schema->column('status')->string(10);
        $schema->column('gateway_name')->string(50);
        $schema->column('description')->text();
        $schema->column('details')->json();
        $schema->column('number')->string(25);
        $this->addTimestamps($schema);

        // Indexes
        $schema->index(['client_id']);
        $schema->index(['status']);
        $schema->index(['number']);

        return $schema;
    }

    public function createForeignKeys(): void
    {
        $table = $this->database->table(self::TABLE_NAME)->getSchema();
        $table->foreignKey(['client_id'])
            ->references(CustomerSchema::TABLE_NAME, ['id'])
            ->onDelete('CASCADE');
        $table->save();
    }
}
