import {FieldType, Renderer, ResourceAction, ResourceConfiguration} from '../cobalt'

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

export default {
    resources: [
        {
            key: 'node',
            primaryKey: 'id',
            collection: 'p5x_nodes',
            display: {
                field: 'hostname',
                singular: 'Instance',
                plural: 'Instances',
            },
            orderField: 'pve_id',
            orderDirection: 'asc',
            supportedActions: [ResourceAction.read, ResourceAction.readOne],
            process: {
                afterRead: tapRef(qr => 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[],
}
