<?php

namespace Modules\Ticket\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddOnlineCancellationFields extends Migration
{
    public function up()
    {
        if (! $this->db->fieldExists('ticket_amount', 'cancels')) {
            $this->forge->addColumn('cancels', [
                'ticket_amount' => [
                    'type'       => 'DECIMAL',
                    'constraint' => '12,2',
                    'null'       => true,
                ],
                'refund_amount' => [
                    'type'       => 'DECIMAL',
                    'constraint' => '12,2',
                    'null'       => true,
                ],
                'status' => [
                    'type'       => 'VARCHAR',
                    'constraint' => '20',
                    'default'    => 'done',
                ],
            ]);
        }

        $db = $this->db;
        $exists = $db->table('menus')
            ->where('menu_title', 'manage_online_cancellations')
            ->where('deleted_at', null)
            ->countAllResults();
        if ($exists > 0) {
            return;
        }

        $parent = $db->table('menus')
            ->select('id')
            ->where('menu_title', 'ticket_booking')
            ->where('deleted_at', null)
            ->get()
            ->getRow();

        if (! $parent) {
            return;
        }

        $db->table('menus')->insert([
            'menu_title'      => 'manage_online_cancellations',
            'page_url'        => 'ticket/booking/onlinecancellations',
            'module_name'     => 'Ticket',
            'parent_menu_id'  => (string) $parent->id,
            'have_chield'     => null,
            'created_by'      => '1',
            'created_at'      => date('Y-m-d H:i:s'),
            'updated_at'      => date('Y-m-d H:i:s'),
            'deleted_at'      => null,
        ]);
        $menuId = $db->insertID();

        $permExists = $db->table('permissions')
            ->where('menu_id', $menuId)
            ->where('role_id', 1)
            ->where('deleted_at', null)
            ->countAllResults();
        if ($permExists > 0) {
            return;
        }

        $db->table('permissions')->insert([
            'role_id'    => 1,
            'user_id'    => 1,
            'menu_id'    => $menuId,
            'menu_title' => 'manage_online_cancellations',
            'create'     => '1',
            'read'       => '1',
            'edit'       => '1',
            'delete'     => '0',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
            'deleted_at' => null,
        ]);
    }

    public function down()
    {
        if ($this->db->fieldExists('ticket_amount', 'cancels')) {
            $this->forge->dropColumn('cancels', ['ticket_amount', 'refund_amount', 'status']);
        }

        $row = $this->db->table('menus')
            ->where('menu_title', 'manage_online_cancellations')
            ->get()
            ->getRow();
        if ($row) {
            $this->db->table('permissions')->where('menu_id', $row->id)->delete();
            $this->db->table('menus')->where('id', $row->id)->delete();
        }
    }
}
