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

use BookiFlex\Extensions\Notification\Model\NotificationStatus;
use BookiFlex\Extensions\Notification\Model\Recipient;
use Cycle\ORM\Parser\CastableInterface;
use Cycle\ORM\Parser\UncastableInterface;

defined( 'ABSPATH' ) || exit;

class NotificationTypecast implements CastableInterface, UncastableInterface
{
    public function setRules(array $rules): array
    {
        // Rules can be set here if necessary
        return $rules;
    }

    public function cast(array $data): array
    {
        if (isset($data['stakeholder']) && is_string($data['stakeholder'])) {
            $data['stakeholder'] = Recipient::from($data['stakeholder']);
        }

        // event stays as string — supports both core and custom event types

        if (isset($data['status']) && is_string($data['status'])) {
            $data['status'] = NotificationStatus::from($data['status']);
        }

        return $data;
    }

    public function uncast(array $data): array
    {
        if (isset($data['stakeholder']) && $data['stakeholder'] instanceof Recipient) {
            $data['stakeholder'] = $data['stakeholder']->value;
        }

        // event is already a string — no uncast needed

        if (isset($data['status']) && $data['status'] instanceof NotificationStatus) {
            $data['status'] = $data['status']->getValue();
        }

        return $data;
    }
}
