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

declare(strict_types=1);

namespace BookiFlex\Extensions\Cart\ORM;

use BookiFlex\Extensions\Cart\Model\Cart;
use BookiFlex\Extensions\Cart\Model\CartItem;
use BookiFlex\Extensions\Cart\ORM\Typecast\CartTypecast;
use BookiFlex\Extensions\Cart\Repository\CartRepository;
use BookiFlex\Extensions\Cart\Repository\CartItemRepository;
use BookiFlex\Core\Model\Customer\Customer;
use Cycle\ORM;

defined( 'ABSPATH' ) || exit;

class CartSchema
{
    public static function getSchema(): array
    {
        return [
            'cart' => [
                ORM\SchemaInterface::MAPPER      => ORM\Mapper\Mapper::class,
                ORM\SchemaInterface::ENTITY      => Cart::class,
                ORM\SchemaInterface::REPOSITORY  => CartRepository::class,
                ORM\SchemaInterface::DATABASE    => 'default',
                ORM\SchemaInterface::TABLE       => 'bflex_carts',
                ORM\SchemaInterface::PRIMARY_KEY => 'id',

                ORM\SchemaInterface::COLUMNS => [
                    'id'             => 'id',
                    'sessionId'      => 'session_id',
                    'currency'       => 'currency',
                    'status'         => 'status',
                    'promoCode'      => 'promo_code',
                    'specialRequest' => 'special_request',
                    'arrivalTime'    => 'arrival_time',
                    'checkInTime'    => 'check_in_time',
                    'checkOutTime'   => 'check_out_time',
                    'customerId'     => 'customer_id',
                    'createdAt'      => 'created_at',
                    'updatedAt'      => 'updated_at',
                ],

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

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

                ORM\SchemaInterface::RELATIONS => [
                    'items' => [
                        ORM\Relation::TYPE   => ORM\Relation::HAS_MANY,
                        ORM\Relation::TARGET => CartItem::class,
                        ORM\Relation::SCHEMA => [
                            ORM\Relation::CASCADE   => true,
                            ORM\Relation::INNER_KEY => ['id'],
                            ORM\Relation::OUTER_KEY => ['cartId'],
                        ],
                    ],
                    'customer' => [
                        ORM\Relation::TYPE   => ORM\Relation::BELONGS_TO,
                        ORM\Relation::TARGET => Customer::class,
                        ORM\Relation::SCHEMA => [
                            ORM\Relation::CASCADE   => false,
                            ORM\Relation::NULLABLE  => true,
                            ORM\Relation::INNER_KEY => ['customerId'],
                            ORM\Relation::OUTER_KEY => ['id'],
                        ],
                    ],
                ],
            ],

            'cartItem' => [
                ORM\SchemaInterface::MAPPER      => ORM\Mapper\Mapper::class,
                ORM\SchemaInterface::ENTITY      => CartItem::class,
                ORM\SchemaInterface::REPOSITORY  => CartItemRepository::class,
                ORM\SchemaInterface::DATABASE    => 'default',
                ORM\SchemaInterface::TABLE       => 'bflex_cart_items',
                ORM\SchemaInterface::PRIMARY_KEY => 'id',

                ORM\SchemaInterface::COLUMNS => [
                    'id'        => 'id',
                    'cartId'    => 'cart_id',
                    'type'      => 'type',
                    'hash'      => 'hash',
                    'quantity'  => 'quantity',
                    'unitPrice' => 'unit_price',
                    'summary'   => 'summary',
                    'data'      => 'data',
                    'sortOrder' => 'sort_order',
                    'createdAt' => 'created_at',
                    'updatedAt' => 'updated_at',
                ],

                ORM\SchemaInterface::TYPECAST => [
                    'id'        => 'int',
                    'cartId'    => 'int',
                    'quantity'  => 'int',
                    'unitPrice' => 'int',
                    'data'      => 'json',
                    'summary'   => 'json',
                    'sortOrder' => 'int',
                    'createdAt' => 'datetime',
                    'updatedAt' => 'datetime',
                ],

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

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