<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

/**
 * Unified table for both products (inventory / goods) and services.
 * Both can be used as line items on quotes and invoices.
 */
final class CreateProductsServices extends AbstractMigration
{
    public function change(): void
    {
        $this->table('products_services', ['id' => 'item_id', 'signed' => false])
            ->addColumn('tenant_id',    'integer', ['signed' => false])
            ->addColumn('type',         'string',  ['limit' => 20, 'comment' => 'product or service'])
            ->addColumn('name',         'string',  ['limit' => 200])
            ->addColumn('sku',          'string',  ['limit' => 80,  'null' => true])
            ->addColumn('description',  'text',    ['null' => true])
            ->addColumn('unit',         'string',  ['limit' => 30,  'default' => 'ea', 'comment' => 'ea, hr, kg, m, etc.'])
            ->addColumn('unit_cost',    'decimal', ['precision' => 12, 'scale' => 2, 'null' => true])
            ->addColumn('unit_price',   'decimal', ['precision' => 12, 'scale' => 2, 'default' => 0.00])
            ->addColumn('taxable',      'boolean', ['default' => true])
            ->addColumn('tax_rate',     'decimal', ['precision' => 5, 'scale' => 2, 'default' => 0.00])
            ->addColumn('category',     'string',  ['limit' => 100, 'null' => true])
            ->addColumn('is_active',    'boolean', ['default' => true])
            ->addColumn('created_at',   'datetime',['default' => 'CURRENT_TIMESTAMP'])
            ->addColumn('updated_at',   'datetime',['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP'])
            ->addColumn('deleted_at',   'datetime',['null' => true])
            ->addIndex(['tenant_id', 'type'])
            ->addIndex(['tenant_id', 'category'])
            ->addIndex(['tenant_id', 'is_active'])
            ->addForeignKey('tenant_id', 'tenants', 'tenant_id', ['delete' => 'RESTRICT', 'update' => 'CASCADE'])
            ->create();
    }
}