<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddPeriodicityEquipoAndDeliveryNotePhotos extends Migration
{
    public function up()
    {
        $this->forge->addColumn('inspections', [
            'periodicidad' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
                'default' => 'mensual',
                'after' => 'fecha',
            ],
        ]);

        $this->forge->addColumn('inspection_template_items', [
            'equipo' => [
                'type' => 'VARCHAR',
                'constraint' => 160,
                'null' => true,
                'after' => 'pregunta',
            ],
        ]);

        $this->db->table('inspection_template_items')
            ->where('frecuencia', 'Q')
            ->update(['frecuencia' => 'S']);

        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'delivery_note_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'section' => ['type' => 'VARCHAR', 'constraint' => 40, 'default' => 'general'],
            'caption' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'file_path' => ['type' => 'VARCHAR', 'constraint' => 255],
            'mime' => ['type' => 'VARCHAR', 'constraint' => 120, 'null' => true],
            'size' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'orden' => ['type' => 'INT', 'constraint' => 11, 'default' => 0],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('delivery_note_id');
        $this->forge->addForeignKey('delivery_note_id', 'delivery_notes', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('delivery_note_photos');
    }

    public function down()
    {
        $this->forge->dropTable('delivery_note_photos', true);
        $this->forge->dropColumn('inspection_template_items', 'equipo');
        $this->forge->dropColumn('inspections', 'periodicidad');
    }
}
