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

declare(strict_types=1);

namespace BookiFlex\Extensions\Notification\ORM\Schema;

use BookiFlex\Core\ORM\Schema\AbstractSchema;
use BookiFlex\Core\ORM\Schema\ReservationSchema;
use BookiFlex\Extensions\Notification\Model\NotificationStatus;
use Cycle\Database\Schema\AbstractTable;

defined( 'ABSPATH' ) || exit;

/**
 * Notification table schema
 */
class NotificationSchema extends AbstractSchema
{
    public const TABLE_NAME = 'bflex_notifications';

    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('sid')->string(36);
        $schema->column('stakeholder')->string(25);
        $schema->column('event')->string(25);
        $schema->column('send_to')->string(50);
        $schema->column('send_time')->datetime();
        $schema->column('transport')->string(50);
        $schema->column('attempt')->tinyInteger()->unsigned(true);
        $schema->column('status')->string(20)->defaultValue(NotificationStatus::PENDING);
        $schema->column('details')->json();
        $schema->column('reservation_id')->integer();
        $this->addTimestamps($schema);

        // Indexes
        $schema->index(['sid']);

        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();
    }
}
