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

declare(strict_types=1);

namespace BookiFlex\Core\ORM\Schema;

use Cycle\Database\Schema\AbstractTable;

defined( 'ABSPATH' ) || exit;

/**
 * Reservation extra items table schema
 */
class ReservationExtraSchema extends AbstractSchema
{
    public const TABLE_NAME = 'bflex_reservation_extra_items';

    public function define(): AbstractTable
    {
        // This returns the main reservation table
        // Other tables are created via additional methods
        return $this->defineReservationTable();
    }

    protected function defineReservationTable(): AbstractTable
    {
        $schema = $this->database->table(self::TABLE_NAME)->getSchema();
        $schema->column('id')->integer()->primary();
        $schema->column('date')->date()->nullable()->defaultValue(null);
        $schema->column('base_price')->integer();
        $schema->column('total_price')->integer();
        $schema->column('currency')->string(3);
        $schema->column('quantity')->tinyInteger();
        $schema->column('neutral')->boolean()->defaultValue(false);
        $schema->column('name')->string(100);
        $schema->column('code')->string(20);
        $schema->column('details')->json();
        $schema->column('reservation_id')->integer();
        $this->addTimestamps($schema);

        return $schema;
    }

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