<?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;

/**
 * Date unit table schema
 */
class DateUnitSchema extends AbstractSchema
{
    public const TABLE_NAME = 'bflex_date_units';

    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('accommodation_type_id')->bigInteger()
            ->unsigned(true);
        $schema->column('rate_plan_id')->bigInteger()
            ->unsigned(true);
        $schema->column('price')->json();
        $schema->column('stop_sale')->boolean()->defaultValue(false);
        $schema->column('min_los')->integer()->nullable()->defaultValue(null);
        $schema->column('max_los')->integer()->nullable()->defaultValue(null);
        $schema->column('min_los_arrival')->integer()->nullable()->defaultValue(null);
        $schema->column('max_los_arrival')->integer()->nullable()->defaultValue(null);
        $schema->column('min_adv_booking')->integer()->nullable()->defaultValue(null);
        $schema->column('max_adv_booking')->integer()->nullable()->defaultValue(null);
        $schema->column('cta')->tinyInteger()->defaultValue(0);
        $schema->column('ctd')->tinyInteger()->defaultValue(0);
        $schema->column('date')->date();

        $this->addTimestamps($schema);

        // Indexes
        $schema->index(['rate_plan_id']);
        $schema->index(['accommodation_type_id', 'rate_plan_id', 'date']);

        return $schema;
    }
}
