<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('mr_master_response_templates');
        Schema::create('mr_master_response_templates', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('mid');
            $table->string('code');
            $table->string('type');
            $table->string('name');
            $table->string('description');
            $table->text('template');
            $table->timestamps();

            // Foreign key constraint
            // onDelete('cascade'); means when delete data from main table, sub table will be deleted too.
            $table->foreign('mid')->references('id')->on('master_messages')->onDelete('cascade');
        });

        DB::table('mr_master_response_templates')->insert([
            //Export
            [
                'mid' => 1,
                'code' => '0209',
                'type' => 'XDCA',
                'name' => 'DECLARATION ACCEPTED',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"DECLARATION ACCEPTED"}}}}'
            ],
            [
                'mid' => 1,
                'code' => '0300',
                'type' => 'GCLD',
                'name' => 'พบหน่วยงานผู้ออกใบอนุญาตหรือใบรับรองอิเล็กทรอนิกส์เพื่อตรวจสอบของ',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"0010","Message":"พบหน่วยงานผู้ออกใบอนุญาตหรือใบรับรองอิเล็กทรอนิกส์เพื่อตรวจสอบของ","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 1,
                'code' => '0301',
                'type' => 'GCLD',
                'name' => 'Goods Transition Control Already Checked ให้มาตรวจของที่ท่าที่ส่งออก',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"0010","Message":"Goods Transition Control Already Checked ให้มาตรวจของที่ท่าที่ส่งออก","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 1,
                'code' => '0309',
                'type' => 'GCLD',
                'name' => 'Goods Transition Control Already Checked',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"0010","Message":"Goods Transition Control Already Checked","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 1,
                'code' => '0409',
                'type' => 'GCLD',
                'name' => 'GOODS LOADED',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONCONT1234567","LoadPort":"0010","Message":"GOODS LOADED","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 1,
                'code' => 'REJECT',
                'type' => 'XDCR',
                'name' => 'REJECT',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XDCR","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationReject":{"ItemNumber":"0","ErrorCode":"GDEX-00044","Message":"INVALID DEPARTURE DATE"}}}}'
            ],
            [
                'mid' => 1,
                'code' => 'CANCEL ACCEPTED',
                'type' => 'XCDA',
                'name' => 'DECLARATION CANCELED',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XCDA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"CancelDocumentAccept":{"DocumentNumber":"","Message":"DECLARATION CANCELED"}}}}'
            ],
            [
                'mid' => 1,
                'code' => 'CANCEL REJECT',
                'type' => 'XCDR',
                'name' => 'INVALID DECLARATION NUMBER',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XCDR","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"CancelDocumentReject":{"DocumentNumber":"","ErrorCode":"CLGD-00016","Message":"DECLARATION STATUS CAN NOT CANCEL"}}}}'
            ],
            [
                'mid' => 1,
                'code' => '9904',
                'type' => 'GCLD',
                'name' => 'Goods Transition Control Already Cancel Matching',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"0010","Message":"Goods Transition Control Already Cancel Matching","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 1,
                'code' => 'ERR_TRANS',
                'type' => 'ERR_TRANS',
                'name' => 'Error Trans',
                'description' => '',
                'template' => '{"root":"ErrorMessage","namespace":null,"DocumentDetails":{"TransactionNo":"E06@240209210907_418","ReferenceNo":"","ErrorType":"ERR_TRANS","ErrorItems":"1","ErrorDate":"","ErrorDescription":"Incorrect length = 0. Facet length = 2. element=\'CustomsExportDeclaration[1]/SEQUENCE1[1]/DocumentControl[1]/SEQUENCE1[1]/TotalPackage[1]/SEQUENCE1[1]/UnitCode\'.  At line = 1536 column = 1."}}'
            ],

            //Import
            [
                'mid' => 2,
                'code' => '0109',
                'type' => 'IDCA',
                'name' => 'พร้อมที่จะชำระเงินค่าภาษีอากรที่หน่วยงานของกรมศุลกากร',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"พร้อมที่จะชำระเงินค่าภาษีอากรที่หน่วยงานของกรมศุลกากร"}}}}'
            ],
            [
                'mid' => 2,
                'code' => '0209',
                'type' => 'IDCA',
                'name' => 'DECLARATION ACCEPTED',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"DECLARATION ACCEPTED"}}}}'
            ],
            [
                'mid' => 2,
                'code' => '0209',
                'type' => 'IDCA',
                'name' => 'รอการตัดบัญชีธนาคาร',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"รอการตัดบัญชีธนาคาร"}}}}'
            ],
            [
                'mid' => 2,
                'code' => '0209',
                'type' => 'PMTA',
                'name' => 'ได้รับการชำระเงินเรียบร้อยแล้ว',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"PMTA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationPayment":{"DeclarationNumber":"","Status":"A","PaymentType":"PMT","Message":"ได้รับการชำระเงินเรียบร้อยแล้ว","PaymentNumber":"123456","BankTransactionNumber":"FCN0000000001","TotalAmount":"1000"}}}}'
            ],
            [
                'mid' => 2,
                'code' => '0301',
                'type' => 'IDCA',
                'name' => 'สินค้าต้องตรวจสอบ',
                'description' => 'R',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"สินค้าต้องตรวจสอบ"}}}}'
            ],
            [
                'mid' => 2,
                'code' => '0309',
                'type' => 'IDCA',
                'name' => 'ไม่ต้องตรวจสอบพิกัดศุลกากร ราคาและของ ไปรับของ ที่ท่าหรือที่นำเข้า',
                'description' => 'G',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"ไม่ต้องตรวจสอบพิกัดศุลกากร ราคาและของ ไปรับของ ที่ท่าหรือที่นำเข้า"}}}}'
            ],
            [
                'mid' => 2,
                'code' => '0409',
                'type' => 'IDCA',
                'name' => 'ส่งมอบสินค้า',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationAccept":{"Status":"00","DeclarationNumber":"","TotalTax":"1000","TotalDeposit":"0","ReleasePort":"1193","Message":"ส่งมอบสินค้า"}}}}'
            ],
            [
                'mid' => 2,
                'code' => 'REJECT',
                'type' => 'IDCR',
                'name' => 'REJECT',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCR","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"DeclarationReject":{"ItemNumber":"0","ErrorCode":"ITEM-00058","Message":"INVALID DISCHARGE PORT"}}}}'
            ],
            [
                'mid' => 2,
                'code' => 'ERR_TRANS',
                'type' => 'ERR_TRANS',
                'name' => 'Error Trans',
                'description' => '',
                'template' => '{"root":"ErrorMessage","namespace":null,"DocumentDetails":{"TransactionNo":"E06@240209210907_418","ReferenceNo":"","ErrorType":"ERR_TRANS","ErrorItems":"1","ErrorDate":"","ErrorDescription":"Value too short, length = 0. Facet minLength = 1. element=\'CustomsImportDeclaration[1]/SEQUENCE1[1]/DocumentControl[1]/SEQUENCE1[1]/BorderTransportMeans[1]/SEQUENCE1[1]/VesselName\'.  At line = 1149 column = 1."}}'
            ],
            [
                'mid' => 2,
                'code' => 'CANCEL ACCEPTED',
                'type' => 'XCDA',
                'name' => 'DECLARATION CANCELED',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XCDA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"CancelDocumentAccept":{"DocumentNumber":"","Message":"DECLARATION CANCELED"}}}}'
            ],
            [
                'mid' => 2,
                'code' => 'CANCEL REJECT',
                'type' => 'XCDR',
                'name' => 'INVALID DECLARATION NUMBER',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XCDR","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"CancelDocumentReject":{"DocumentNumber":"","ErrorCode":"CLGD-00008","Message":"INVALID DECLARATION NUMBER"}}}}'
            ],

            //GCL
            [
                'mid' => 3,
                'code' => '0209',
                'type' => 'GCLA',
                'name' => 'GOODS CONTROL ACCEPT',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsTransitionAccept":{"GoodsTransitionNumber":"","ContainerNumber":"CONT1234567","LoadPort":"2801","Message":"GOODS CONTROL ACCEPT"}}}}'
            ],
            [
                'mid' => 3,
                'code' => '0209',
                'type' => 'GCLA_BKK',
                'name' => 'GOODS CONTROL ACCEPT (Port)',
                'description' => 'PORT',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLA_BKK","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsTransitionAccept":{"GoodsTransitionNumber":"","ContainerNumber":"CONT1234567","LoadPort":"2801","Message":"GOODS CONTROL ACCEPT"}}}}'
            ],
            [
                'mid' => 3,
                'code' => '0301',
                'type' => 'GCLD',
                'name' => 'Goods Transition Control Already Checked ให้มาตรวจของที่ท่าที่ส่งออก',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0041010353980000000010001T5","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"0010","Message":"Goods Transition Control Already Checked ให้มาตรวจของที่ท่าที่ส่งออก","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 3,
                'code' => '0309',
                'type' => 'GCLD',
                'name' => 'Goods Transition Control Already Checked',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0041010353980000000010001T5","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"0010","Message":"Goods Transition Control Already Checked","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 3,
                'code' => 'REJECT',
                'type' => 'GCLR',
                'name' => 'REJECT',
                'description' => 'INVALID CONTAINER SIZE AND TYPE CODE',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLR","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsTransitionReject":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","ErrorCode":"GCEX-00052    ","Message":"INVALID CONTAINER SIZE AND TYPE CODE"}}}}'
            ],
            [
                'mid' => 3,
                'code' => 'CANCEL ACCEPT',
                'type' => 'XCDA',
                'name' => 'CANCEL',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XCDA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"CancelDocumentAccept":{"DocumentNumber":"","Message":"GOODS CONTROL CANCELED"}}}}'
            ],
            [
                'mid' => 3,
                'code' => 'CANCEL ACCEPT - PORT',
                'type' => 'XCDA_LCB',
                'name' => 'CANCEL',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"XCDA_LCB","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"CancelDocumentAccept":{"DocumentNumber":"","Message":"GOODS CONTROL CANCELED"}}}}'
            ],
            [
                'mid' => 3,
                'code' => 'CANCEL MATCHING',
                'type' => 'GCLD',
                'name' => 'CANCEL MATCHING',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"GCLD","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"GoodsAudit":{"DeclarationNumber":"","ContainerNumber":"CONT1234567","LoadPort":"2801","Message":"Goods Transition Control Already Cancel Matching","AuditDateTime":""}}}}'
            ],
            [
                'mid' => 3,
                'code' => 'ERR_TRANS',
                'type' => 'ERR_TRANS',
                'name' => 'Error Trans',
                'description' => '',
                'template' => '{"root":"ErrorMessage","namespace":null,"DocumentDetails":{"TransactionNo":"E06@240209210907_418","ReferenceNo":"","ErrorType":"ERR_TRANS","ErrorItems":"1","ErrorDate":"","ErrorDescription":"Too few occurences of element=\'CustomsGoodsTransition[1]/SEQUENCE1[1]/DocumentControl[1]/SEQUENCE1[1]/ContainerInfo[1]/SEQUENCE1[1]/TaxNumber\'. Found = 0. Min = 1.  At line = 822 column = 1."}}'
            ],

            //SHORT
            [
                'mid' => 4,
                'code' => '9980',
                'type' => 'SDCA',
                'name' => 'SHORT DECLARATION ACCEPTED',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"IDCA","ReferenceNumber":"","RegistrationID":"TH0101075570001010000000001T0","DocumentDetail":{"ShortDeclarationAccept":{"DeclarationNumber":"","Message":"SHORT DECLARATION ACCEPTED"}}}}'
            ],
            [
                'mid' => 4,
                'code' => '0000',
                'type' => 'SDCR',
                'name' => 'SHORT DECLARATION REJECT',
                'description' => '',
                'template' => '{"root":"CustomsResponse","namespace":"http://ebxml.customs.go.th/XMLSchema/CustomsResponse_8_00","DocumentControl":{"MessageType":"SDCR","ReferenceNumber":"","RegistrationID":"TH0041010353980000000010001T5","DocumentDetail":{"ShortDeclarationReject":{"DeclarationNumber":"","DeclarationLineNumber":"0","ErrorCode":"SHEX-00029","Message":"CAN NOT SHORT IF DECLARATION ITEM MORE THAN 1 ITEM AND SOME VALUE IN ITEM = 0"}}}}'
            ],
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('mr_master_response_templates');
    }
};
