<?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 accommodation items table schema
 */
class ReservationAccommodationSchema extends AbstractSchema
{
    public const TABLE_NAME = 'bflex_reservation_accommodation_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('name')->string(100);
        $schema->column('adults')->integer();
        $schema->column('children')->json();
        $schema->column('unit')->string(20)->nullable()->defaultValue(null);
        $schema->column('accommodation_type_code')->string(10);
        $schema->column('rate_plan_code')->string(10);
        $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();
    }
}
