<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

/**
 * Calibration — per-instrument recurring cal schedule + certificate log.
 * Attachments (cert PDFs, images) are stored via the polymorphic attachments
 * table with owner_type='calibration_record'.
 */
final class CreateCalibration extends AbstractMigration
{
    public function change(): void
    {
        $this->table('calibration_schedules', ['id' => 'cal_schedule_id', 'signed' => false])
            ->addColumn('tenant_id',      'integer', ['signed' => false])
            ->addColumn('asset_id',       'integer', ['signed' => false])
            ->addColumn('name',           'string',  ['limit' => 160])
            ->addColumn('standard',       'string',  ['limit' => 100, 'null' => true])
            ->addColumn('interval_days',  'integer', ['default' => 365])
            ->addColumn('tolerance',      'string',  ['limit' => 80,  'null' => true])
            ->addColumn('unit',           'string',  ['limit' => 20,  'null' => true])
            ->addColumn('last_calibrated_at', 'date', ['null' => true])
            ->addColumn('next_due_at',    'date',    ['null' => true])
            ->addColumn('is_active',      'boolean', ['default' => true])
            ->addColumn('notes',          'text',    ['null' => 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'])
            ->addIndex(['tenant_id', 'next_due_at'])
            ->addIndex(['asset_id'])
            ->addForeignKey('tenant_id', 'tenants', 'tenant_id', ['delete' => 'CASCADE', 'update' => 'CASCADE'])
            ->addForeignKey('asset_id',  'assets',  'asset_id',  ['delete' => 'CASCADE', 'update' => 'CASCADE'])
            ->create();

        $this->table('calibration_records', ['id' => 'cal_record_id', 'signed' => false])
            ->addColumn('tenant_id',         'integer', ['signed' => false])
            ->addColumn('cal_schedule_id',   'integer', ['signed' => false])
            ->addColumn('performed_by',      'integer', ['null' => true, 'signed' => false])
            ->addColumn('performed_at',      'datetime',['null' => true])
            ->addColumn('technician_name',   'string',  ['limit' => 120, 'null' => true])
            ->addColumn('as_found',          'string',  ['limit' => 80,  'null' => true])
            ->addColumn('as_left',           'string',  ['limit' => 80,  'null' => true])
            ->addColumn('result',            'string',  ['limit' => 20,  'default' => 'pass'])
            // pass | fail | adjusted
            ->addColumn('certificate_number','string',  ['limit' => 80,  'null' => true])
            ->addColumn('notes',             'text',    ['null' => true])
            ->addColumn('created_at',        'datetime',['default' => 'CURRENT_TIMESTAMP'])
            ->addIndex(['cal_schedule_id', 'performed_at'])
            ->addForeignKey('tenant_id',       'tenants',               'tenant_id',       ['delete' => 'CASCADE', 'update' => 'CASCADE'])
            ->addForeignKey('cal_schedule_id', 'calibration_schedules', 'cal_schedule_id', ['delete' => 'CASCADE', 'update' => 'CASCADE'])
            ->addForeignKey('performed_by',    'users',                 'user_id',         ['delete' => 'SET_NULL', 'update' => 'CASCADE'])
            ->create();
    }
}
