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

use BookiFlex\Core\Model\Reservation\Reservation;
use BookiFlex\Extensions\Notification\ORM\Schema\NotificationSchema;
use BookiFlex\Extensions\Notification\Model\Notification;
use BookiFlex\Extensions\Notification\Repository\NotificationRepository;
use Cycle\ORM;

defined( 'ABSPATH' ) || exit;

class Schema
{
    public static function getSchema(string $prefix = ''): array
    {
        return [
            'notification' => [
                ORM\SchemaInterface::MAPPER      => ORM\Mapper\Mapper::class, // default
                ORM\SchemaInterface::ENTITY      => Notification::class,
                ORM\SchemaInterface::REPOSITORY  => NotificationRepository::class,
                ORM\SchemaInterface::DATABASE    => 'default',
                ORM\SchemaInterface::TABLE       => NotificationSchema::TABLE_NAME,
                ORM\SchemaInterface::PRIMARY_KEY => 'id',
                ORM\SchemaInterface::COLUMNS     => [
                    'id'            => 'id',
                    'sid'           => 'sid',
                    'stakeholder'   => 'stakeholder',
                    'event'         => 'event',
                    'sendTo'        => 'send_to',
                    'sendTime'      => 'send_time',
                    'transport'     => 'transport',
                    'attempt'       => 'attempt',
                    'status'        => 'status',
                    'details'       => 'details',
                    'reservationId' => 'reservation_id',
                    'createdAt'     => 'created_at',
                    'updatedAt'     => 'updated_at',
                ],

                ORM\SchemaInterface::TYPECAST => [
                    'id'          => 'int',
                    'sid'         => 'string',
                    'stakeholder' => 'string',
                    'event'       => 'string',
                    'sendTo'      => 'string',
                    'sendTime'    => 'datetime',
                    'transport'   => 'string',
                    'attempt'     => 'int',
                    'status'      => 'string',
                    'details'     => 'json',
                    'createdAt'   => 'datetime',
                    'updatedAt'   => 'datetime',
                ],

                ORM\SchemaInterface::TYPECAST_HANDLER => [
                    ORM\Parser\Typecast::class,
                    Typecast\NotificationTypecast::class,
                ],

                ORM\SchemaInterface::RELATIONS => [
                    'reservation' => [
                        ORM\Relation::TYPE   => ORM\Relation::BELONGS_TO,
                        ORM\Relation::TARGET => Reservation::class,
                        ORM\Relation::SCHEMA => [
                            ORM\Relation::CASCADE   => false,
                            ORM\Relation::INNER_KEY => ['reservationId'],
                            ORM\Relation::OUTER_KEY => ['id'],
                        ],
                    ],
                ],
            ],
        ];
    }
}
