<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

/**
 * Preventive Maintenance templates + per-asset schedules.
 * A CLI command (bin/console pm:generate) runs daily to convert due
 * schedules into concrete work orders.
 */
final class CreatePmTemplates extends AbstractMigration
{
    public function change(): void
    {
        $this->table('pm_templates', ['id' => 'template_id', 'signed' => false])
            ->addColumn('tenant_id',   'integer', ['signed' => false])
            ->addColumn('asset_type_id','integer',['null' => true, 'signed' => false])
            ->addColumn('code',        'string',  ['limit' => 40,  'null' => true])
            ->addColumn('name',        'string',  ['limit' => 160])
            ->addColumn('description', 'text',    ['null' => true])
            ->addColumn('interval_days','integer',['default' => 30])
            ->addColumn('lead_days',   'integer', ['default' => 3])
            ->addColumn('priority',    'string',  ['limit' => 20, 'default' => 'medium'])
            ->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', 'is_active'])
            ->addForeignKey('tenant_id',    'tenants',     'tenant_id',     ['delete' => 'RESTRICT', 'update' => 'CASCADE'])
            ->addForeignKey('asset_type_id','asset_types', 'asset_type_id', ['delete' => 'SET_NULL', 'update' => 'CASCADE'])
            ->create();

        $this->table('pm_template_tasks', ['id' => 'template_task_id', 'signed' => false])
            ->addColumn('template_id', 'integer', ['signed' => false])
            ->addColumn('sort_order',  'integer', ['default' => 0])
            ->addColumn('title',       'string',  ['limit' => 200])
            ->addColumn('description', 'text',    ['null' => true])
            ->addForeignKey('template_id', 'pm_templates', 'template_id', ['delete' => 'CASCADE', 'update' => 'CASCADE'])
            ->create();

        // Per-asset schedules — links a template to one asset with a running due_at.
        $this->table('pm_schedules', ['id' => 'schedule_id', 'signed' => false])
            ->addColumn('tenant_id',   'integer', ['signed' => false])
            ->addColumn('template_id', 'integer', ['signed' => false])
            ->addColumn('asset_id',    'integer', ['signed' => false])
            ->addColumn('assigned_to', 'integer', ['null' => true, 'signed' => false])
            ->addColumn('next_due_at', 'datetime',['null' => true])
            ->addColumn('last_generated_at','datetime',['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'])
            ->addIndex(['tenant_id', 'is_active'])
            ->addIndex(['next_due_at'])
            ->addForeignKey('tenant_id',   'tenants',      'tenant_id',   ['delete' => 'RESTRICT', 'update' => 'CASCADE'])
            ->addForeignKey('template_id', 'pm_templates', 'template_id', ['delete' => 'CASCADE',  'update' => 'CASCADE'])
            ->addForeignKey('asset_id',    'assets',       'asset_id',    ['delete' => 'CASCADE',  'update' => 'CASCADE'])
            ->addForeignKey('assigned_to', 'users',        'user_id',     ['delete' => 'SET_NULL', 'update' => 'CASCADE'])
            ->create();
    }
}
