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

declare(strict_types=1);

 namespace BookiFlex\Extensions\Payment\ORM;

use BookiFlex\Core\Model\Customer\Customer;
use BookiFlex\Core\Model\Reservation\Reservation;
use BookiFlex\Extensions\Payment\Model\Payment;
use BookiFlex\Extensions\Payment\Model\PaymentAllocation;
use BookiFlex\Extensions\Payment\Model\PaymentAllocationType;
use BookiFlex\Extensions\Payment\Model\PaymentToken;
use BookiFlex\Extensions\Payment\Repository\PaymentAllocationRepository;
use BookiFlex\Extensions\Payment\Repository\PaymentRepository;
use Cycle\ORM;

defined( 'ABSPATH' ) || exit;

class Schema
{
    public static function getSchema(string $prefix = ''): array
    {
        return [
            'payment' => [
                ORM\SchemaInterface::MAPPER      => ORM\Mapper\Mapper::class, // default
                ORM\SchemaInterface::ENTITY      => Payment::class,
                ORM\SchemaInterface::REPOSITORY  => PaymentRepository::class,
                ORM\SchemaInterface::DATABASE    => 'default',
                ORM\SchemaInterface::TABLE       => Schema\PaymentSchema::TABLE_NAME,
                ORM\SchemaInterface::PRIMARY_KEY => 'id',
                ORM\SchemaInterface::COLUMNS     => [
                    'id'           => 'id',
                    'clientId'     => 'client_id',
                    'totalAmount'  => 'total_amount',
                    'currencyCode' => 'currency_code',
                    'status'       => 'status',
                    'gatewayName'  => 'gateway_name',
                    'description'  => 'description',
                    'details'      => 'details',
                    'number'       => 'number',
                    'createdAt'    => 'created_at',
                    'updatedAt'    => 'updated_at',
                ],

                ORM\SchemaInterface::TYPECAST => [
                    'id'           => 'int',
                    'totalAmount'  => 'int',
                    'currencyCode' => 'string',
                    'gatewayName'  => 'string',
                    'description'  => 'string',
                    'number'       => 'string',
                    'details'      => 'json',
                    'createdAt'    => 'datetime',
                    'updatedAt'    => 'datetime',
                    'status'       => 'string',
                ],

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

                ORM\SchemaInterface::RELATIONS => [
                    'client' => [
                        ORM\Relation::TYPE   => ORM\Relation::BELONGS_TO,
                        ORM\Relation::TARGET => Customer::class,
                        ORM\Relation::SCHEMA => [
                            ORM\Relation::CASCADE   => false,
                            ORM\Relation::INNER_KEY => ['clientId'],
                            ORM\Relation::OUTER_KEY => ['id'],
                        ],
                    ],
                    'paymentAllocations' => [
                        ORM\Relation::TYPE   => ORM\Relation::HAS_MANY,
                        ORM\Relation::TARGET => 'paymentAllocation',
                        ORM\Relation::SCHEMA => [
                            ORM\Relation::CASCADE   => true,
                            ORM\Relation::INNER_KEY => ['id'],
                            ORM\Relation::OUTER_KEY => ['paymentId'],
                        ],
                    ],
                ],
            ],
            'paymentAllocation' => [
                ORM\SchemaInterface::MAPPER      => ORM\Mapper\Mapper::class, // default
                ORM\SchemaInterface::ENTITY      => PaymentAllocation::class,
                ORM\SchemaInterface::REPOSITORY  => PaymentAllocationRepository::class,
                ORM\SchemaInterface::DATABASE    => 'default',
                ORM\SchemaInterface::TABLE       => Schema\PaymentAllocationSchema::TABLE_NAME,
                ORM\SchemaInterface::PRIMARY_KEY => 'id',
                ORM\SchemaInterface::COLUMNS     => [
                    'id'            => 'id',
                    'type'          => 'type',
                    'paymentId'     => 'payment_id',
                    'reservationId' => 'reservation_id',
                    'amount'        => 'amount',
                    'currency'      => 'currency',
                    'createdAt'     => 'created_at',
                    'updatedAt'     => 'updated_at',
                ],

                ORM\SchemaInterface::TYPECAST => [
                    'id'        => 'int',
                    'amount'    => 'int',
                    'createdAt' => 'datetime',
                    'updatedAt' => 'datetime',
                    'type'      => PaymentAllocationType::class
                ],

                ORM\SchemaInterface::RELATIONS => [
                    'payment' => [
                        ORM\Relation::TYPE   => ORM\Relation::BELONGS_TO,
                        ORM\Relation::TARGET => Payment::class,
                        ORM\Relation::SCHEMA => [
                            ORM\Relation::CASCADE   => true,
                            ORM\Relation::INNER_KEY => ['paymentId'],
                            ORM\Relation::OUTER_KEY => ['id'],
                        ],
                    ],
                    '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'],
                        ],
                    ],
                ],
            ],
            'paymentToken' => [
                ORM\SchemaInterface::MAPPER      => ORM\Mapper\Mapper::class, // default
                ORM\SchemaInterface::ENTITY      => PaymentToken::class,
                ORM\SchemaInterface::REPOSITORY  => PaymentAllocationRepository::class,
                ORM\SchemaInterface::DATABASE    => 'default',
                ORM\SchemaInterface::TABLE       => Schema\PaymentTokenSchema::TABLE_NAME,
                ORM\SchemaInterface::PRIMARY_KEY => 'hash',
                ORM\SchemaInterface::COLUMNS     => [
                    'hash'        => 'hash',
                    'details'     => 'details',
                    'afterUrl'    => 'after_url',
                    'targetUrl'   => 'target_url',
                    'gatewayName' => 'gateway_name',
                    'createdAt'   => 'created_at',
                    'updatedAt'   => 'updated_at',
                ],

                ORM\SchemaInterface::TYPECAST => [
                    'hash'        => 'string',
                    'afterUrl'    => 'string',
                    'targetUrl'   => 'string',
                    'gatewayName' => 'string',
                    'createdAt'   => 'datetime',
                    'updatedAt'   => 'datetime',
                ],

                ORM\SchemaInterface::TYPECAST_HANDLER => [
                    ORM\Parser\Typecast::class,
                    Typecast\PaymentTokenIdentityTypecast::class,
                ],
            ]
        ];
    }
}
