import {allResourceActions, FieldType, Renderer, ResourceAction, ResourceConfiguration} from '../cobalt'
import {constructable, QueryRow} from '@extollo/lib'
import {HostGroups} from '../http/controllers/resource/HostGroups.controller'

const tapRef = <T>(op: ((t: T) => unknown)): ((opd: T) => T) => {
    return (opd: T): T => {
        op(opd)
        return opd
    }
}

export default {
    resources: [
        {
            key: 'hostgroup',
            primaryKey: 'id',
            source: { controller: constructable(HostGroups) },
            display: {
                field: 'name',
                singular: 'Host Group',
                plural: 'Host Groups',
            },
            orderField: 'name',
            orderDirection: 'asc',
            supportedActions: allResourceActions,
            fields: [
                {
                    key: 'id',
                    display: 'Host Group ID',
                    type: FieldType.number,
                    readonly: true,
                    required: false,
                },
                {
                    key: 'name',
                    display: 'Name',
                    type: FieldType.text,
                    required: true,
                },
                {
                    key: 'hosts',
                    display: 'Physical Hosts',
                    type: FieldType.select,
                    required: true,
                    displayFrom: 'pveDisplay',
                    valueFrom: 'pveHost',
                    hideOn: {
                        listing: true,
                    },
                    source: {
                        method: constructable<HostGroups>(HostGroups)
                            .tap(c => c.getBoundMethod('getPVEHosts')),
                    },
                },
            ],
        },
        {
            key: 'node',
            primaryKey: 'id',
            source: {
                collection: 'p5x_nodes',
            },
            display: {
                field: 'hostname',
                singular: 'Instance',
                plural: 'Instances',
            },
            orderField: 'pve_id',
            orderDirection: 'asc',
            supportedActions: [ResourceAction.read, ResourceAction.readOne],
            otherActions: [
                {
                    slug: 'provision',
                    title: 'Provision',
                    color: 'success',
                    icon: 'fa-plus',
                    type: 'route',
                    route: '/dash/provision',
                    overall: true,
                },
            ],
            processAfterRead: tapRef((qr: QueryRow) => qr.ip_display = `<pre><code>${qr.assigned_ip}/${qr.assigned_subnet}</code></pre>`),
            fields: [
                {
                    key: 'id',
                    display: 'Instance ID',
                    type: FieldType.text,
                },
                {
                    key: 'pve_id',
                    display: 'Proxmox ID',
                    type: FieldType.text,
                    hideOn: {
                        listing: true,
                    },
                },
                {
                    key: 'hostname',
                    display: 'Hostname',
                    type: FieldType.text,
                },
                {
                    key: 'assigned_ip',
                    display: 'IP Address',
                    type: FieldType.text,
                    hideOn: {
                        listing: true,
                    },
                },
                {
                    key: 'ip_display',
                    display: 'IP Address',
                    type: FieldType.html,
                    renderer: Renderer.html,
                    queryable: false,
                    hideOn: {
                        form: true,
                    },
                },
                {
                    key: 'assigned_subnet',
                    display: 'Subnet',
                    type: FieldType.integer,
                    hideOn: {
                        listing: true,
                    },
                },
                {
                    key: 'is_permanent',
                    display: 'Permanent?',
                    type: FieldType.bool,
                    renderer: Renderer.bool,
                    hideOn: {
                        listing: true,
                    },
                },
                {
                    key: 'is_master',
                    display: 'Master node?',
                    type: FieldType.bool,
                    renderer: Renderer.bool,
                    hideOn: {
                        listing: true,
                    },
                }
            ],
        },
    ] as ResourceConfiguration[],
}
