import Role from '#models/role'
import Permission from '#models/permission'
import { BaseSeeder } from '@adonisjs/lucid/seeders'

export default class extends BaseSeeder {
    async run() {
        // Get roles
        const adminRole = await Role.findByOrFail('slug', 'admin')
        const managerRole = await Role.findByOrFail('slug', 'manager')
        const operatorRole = await Role.findByOrFail('slug', 'operator')

        // Get permissions
        const createUsers = await Permission.findByOrFail('slug', 'create-users')
        const listarUtilizador = await Permission.findByOrFail('slug', 'listar-utilizador')
        const editUsers = await Permission.findByOrFail('slug', 'edit-users')
        const deleteUsers = await Permission.findByOrFail('slug', 'delete-users')
        const manageTalhoes = await Permission.findByOrFail('slug', 'manage-talhoes')
        const managePivos = await Permission.findByOrFail('slug', 'manage-pivos')
        const manageProdutos = await Permission.findByOrFail('slug', 'manage-produtos')
        const manageClientes = await Permission.findByOrFail('slug', 'manage-clientes')
        const manageProformas = await Permission.findByOrFail('slug', 'manage-proformas')

        // Assign all permissions to admin
        await adminRole.related('permissions').attach([
            createUsers.id,
            listarUtilizador.id,
            editUsers.id,
            deleteUsers.id,
            manageTalhoes.id,
            managePivos.id,
            manageProdutos.id,
            manageClientes.id,
            manageProformas.id,
        ])

        // Assign limited permissions to manager
        await managerRole.related('permissions').attach([
            listarUtilizador.id,
            editUsers.id,
            manageTalhoes.id,
            managePivos.id,
            manageProdutos.id,
            manageClientes.id,
            manageProformas.id,
        ])

        // Assign basic permissions to operator
        await operatorRole.related('permissions').attach([
            listarUtilizador.id,
            manageTalhoes.id,
            managePivos.id,
        ])
    }
}
