<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

/**
 * io-product V3 — `products` catalogue table. Prices/costs are integer
 * minor units; boolean-ish flags are SMALLINT (TINYINT hydrates as bool).
 * Idempotent (skipped if the table already exists).
 */
final class InstallProducts extends AbstractMigration
{
    public function up(): void
    {
        if ($this->hasTable('products')) {
            return;
        }

        $this->execute("CREATE TABLE `products` (
  `id`                 INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `code`               VARCHAR(60)  NOT NULL,
  `name`               VARCHAR(200) NOT NULL,
  `description`        TEXT             NULL,
  `kind`               VARCHAR(20)  NOT NULL DEFAULT 'service',
  `unit_price`         BIGINT       NOT NULL DEFAULT 0,
  `unit_cost`          BIGINT           NULL,
  `currency`           CHAR(3)      NOT NULL DEFAULT 'GBP',
  `default_markup_pct` DECIMAL(7,2)     NULL,
  `is_sold`            SMALLINT     NOT NULL DEFAULT 1,
  `is_purchased`       SMALLINT     NOT NULL DEFAULT 0,
  `is_active`          SMALLINT     NOT NULL DEFAULT 1,
  `parent_id`          INT UNSIGNED     NULL,
  `sort_order`         INT          NOT NULL DEFAULT 0,
  `created`            INT UNSIGNED NOT NULL DEFAULT 0,
  `created_by`         INT UNSIGNED     NULL,
  `modified`           INT UNSIGNED NOT NULL DEFAULT 0,
  `modified_by`        INT UNSIGNED     NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `code` (`code`),
  KEY `parent_id` (`parent_id`),
  KEY `is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    }

    public function down(): void
    {
        $this->execute("DROP TABLE IF EXISTS `products`");
    }
}
