import type { HttpContext } from '@adonisjs/core/http';
import MimeType from '#models/mime_type';

export default class MimetypeController {
    public async index({ auth, inertia }: HttpContext) {
        const direction = 'asc'; // or 'desc'
        const mimetypes  = await MimeType.query().orderBy('name', direction).exec();

        return inertia.render('Admin/Mimetype/Index', {
            mimetypes: mimetypes,
            can: {
                edit: await auth.user?.can(['settings']),
            },
        });
    }

    public async down({ request, response }: HttpContext) {
        const id = request.param('id');
        const mimetype  = await MimeType.findOrFail(id);
        mimetype.enabled = false;
        await mimetype .save();

        // session.flash({ message: 'person has been deactivated!' });
        return response.flash('mimetype  has been deactivated!', 'message').toRoute('settings.mimetype.index')
    }

    public async up({ request, response }: HttpContext) {
        const id = request.param('id');
        const mimetype  = await MimeType.findOrFail(id);
        mimetype.enabled = true;
        await mimetype .save();

        // session.flash({ message: 'person has been activated!' });
        return response.flash('mimetype  has been activated!', 'message').toRoute('settings.mimetype.index');
    }

    // public async edit({ request, inertia }: HttpContext) {
    //     const id = request.param('id');
    //     const license = await License.query().where('id', id).firstOrFail();

    //     // const permissions = await Permission.query().pluck('name', 'id');
    //     // // const userHasRoles = user.roles;
    //     // const rolerHasPermissions = await role.related('permissions').query().orderBy('name').pluck('id');

    //     return inertia.render('Admin/License/Edit', {
    //         // permissions: permissions,
    //         license: license,
    //         // roleHasPermissions: Object.keys(rolerHasPermissions).map((key) => rolerHasPermissions[key]), //convert object to array with role ids
    //     });
    // }
}
