<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;
use CodeIgniter\Database\RawSql;

class OrdenTrabajoTable extends Migration
{
    protected array $COLUMNS = [
        "id" => [
            "type" => "INT",
            "unsigned" => true,
            "auto_increment" => true
        ],
        "pedido_id" => [
            "type" => "INT",
            "unsigned" => true,
            "constraint" => 16,
            "comment" => "Pedido original asociado",
        ],
        "user_created_id" => [
            "type" => "INT",
            "unsigned" => true,
            "constraint" => 10,
            "comment" => "Usuario que ha pasado el pedido a producción",
        ],

        "user_updated_id" => [
            "type" => "INT",
            "unsigned" => true,
            "constraint" => 10,
            "comment" => "Usuario que ha actualizado la orden de trabajo",

        ],
        "fecha_entrega_warning" => [
            "type" => "BOOLEAN",
            "default" => false
        ],
        "fecha_entrega_warning_revised" => [
            "type" => "BOOLEAN",
            "default" => false
        ],
        "total_tirada" => [
            "type" => "DOUBLE",
            "null" => true
        ],
        "total_precio" => [
            "type" => "DOUBLE",
            "null" => true
        ],
        "tipo_entrada" => [
            "type" => "ENUM",
            "constraint" => ["in", "out"],
            "default" => "out",
            "comment" => "Tipo de entrada"
        ],

        "progreso" => [
            "type" => "DOUBLE",
            "default" => 0.00,
            "comment" => "Progreso de 0 a 100"
        ],

        "estado" => [
            "type" => "ENUM",
            "constraint" => ["I", "F", "E","PM"],
            "default" => "I",
            "comment" => "I => INICIADO, F => FINALIZADO, P => PENDIENTE , PM=> PENDIENTE_MATERIAL , E => ERROR"
        ],
        /**============================================
         *               PREIMPRESION
         *=============================================**/
        "revisar_formato" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision del formato"
        ],
        "revisar_lomo" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision del lomo"
        ],
        "revisar_solapa" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision de la solapa"

        ],
        "revisar_isbn" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision del ISBN"

        ],
        "revisar_codigo_barras" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision del codigo de barras"

        ],
        "realizar_imposicion" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision imposicion"

        ],
        "enviar_impresion" => [
            "type" => "BOOLEAN",
            "default" => false,
            "comment" => "Realizar revision impresion"

        ],
        "portada_path" => [
            "type" => "TEXT",
            "default" => null,
            "null" => true,
            "comment" => "Path al archivo de portada"

        ],
        "comentarios" => [
            "type" => "TEXT",
            "null" => true,
            "default" => null,
            "comment" => "Comentarios orden de trabajo"
        ]
    ];
    public function up()
    {
        $this->forge->addField($this->COLUMNS);
        $currenttime = new RawSql("CURRENT_TIMESTAMP");
        $this->forge->addField([
            "created_at" => [
                "type" => "TIMESTAMP",
                "default" => $currenttime,

            ],
            "updated_at" => [
                "type" => "TIMESTAMP",
                "null" => true,

            ],
            "deleted_at" => [
                "type" => "TIMESTAMP",
                "null" => true,

            ],
        ]);
        $this->forge->addPrimaryKey("id");
        $this->forge->addForeignKey("pedido_id", "pedidos", "id");
        $this->forge->addForeignKey("user_created_id", "users", "id");
        $this->forge->addForeignKey("user_updated_id", "users", "id");

        $this->forge->createTable("ordenes_trabajo", true);
    }

    public function down()
    {
        $this->forge->dropTable("ordenes_trabajo");
    }
}
