<?php

declare(strict_types=1);

namespace apexl\Io\modules\product\Entity;

use apexl\entityCore\traits\HasCasts;
use apexl\Io\includes\Entity;
use apexl\Io\interfaces\HasEntityFactory;
use apexl\Io\modules\product\EntityFactory\ProductEntityFactory;

/**
 * A catalogue product (V3). Prices/costs are integer minor units (e.g.
 * pence). `kind` is a free, host-meaningful classification (service /
 * material / expense / bundle). `parent_id` links a variant to its parent.
 *
 * Boolean-ish flags are SMALLINT (the mysql driver hydrates TINYINT as bool).
 *
 * @property int         $id
 * @property string      $code
 * @property string      $name
 * @property string|null $description
 * @property string      $kind
 * @property int         $unit_price          minor units (sell)
 * @property int|null    $unit_cost           minor units
 * @property string      $currency            ISO 4217, e.g. GBP
 * @property string|null $default_markup_pct
 * @property int         $is_sold
 * @property int         $is_purchased
 * @property int         $is_active
 * @property int|null    $parent_id
 * @property int         $sort_order
 * @property int         $created
 * @property int|null    $created_by
 * @property int         $modified
 * @property int|null    $modified_by
 */
class Product extends Entity implements HasEntityFactory
{
    use HasCasts;

    public final const string TABLE = 'products';

    public function __construct()
    {
        parent::__construct(static::TABLE);
    }

    public static function entityFactoryClass(): string
    {
        return ProductEntityFactory::class;
    }

    /** Default markup as a float percentage (the column may hydrate as a string). */
    public function markupPercent(): ?float
    {
        return $this->default_markup_pct !== null ? (float) $this->default_markup_pct : null;
    }
}
