import assert from 'node:assert';
import AuditSource from '../auditsource.js';
import BoxError from '../boxerror.js';
import dockerRegistries from '../dockerregistries.js';
import { HttpError } from '@cloudron/connect-lastmile';
import { HttpSuccess } from '@cloudron/connect-lastmile';
import safe from 'safetydance';


async function load(req, res, next) {
    assert.strictEqual(typeof req.params.id, 'string');

    const [error, result] = await safe(dockerRegistries.get(req.params.id));
    if (error) return next(BoxError.toHttpError(error));
    if (!result) return next(new HttpError(404, 'Registry not found'));
    req.resource = result;

    next();
}

async function list(req, res, next) {
    assert.strictEqual(typeof req.user, 'object');

    const [error, results] = await safe(dockerRegistries.list());
    if (error) return next(BoxError.toHttpError(error));

    next(new HttpSuccess(200, { registries: results.map(dockerRegistries.removePrivateFields) }));
}

function get(req, res, next) {
    assert.strictEqual(typeof req.resource, 'object');
    assert.strictEqual(typeof req.user, 'object');

    next(new HttpSuccess(200, dockerRegistries.removePrivateFields(req.resource)));
}

async function add(req, res, next) {
    assert.strictEqual(typeof req.body, 'object');

    if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
    if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
    if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
    if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a string'));
    if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));

    const [error, id] = await safe(dockerRegistries.add(req.body, AuditSource.fromRequest(req)));
    if (error) return next(BoxError.toHttpError(error));

    next(new HttpSuccess(201, { id }));
}

async function update(req, res, next) {
    assert.strictEqual(typeof req.params.id, 'string');
    assert.strictEqual(typeof req.body, 'object');
    assert.strictEqual(typeof req.resource, 'object');

    if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
    if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
    if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
    if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a string'));
    if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));

    const [updateError] = await safe(dockerRegistries.update(req.resource, req.body, AuditSource.fromRequest(req)));
    if (updateError) return next(BoxError.toHttpError(updateError));

    next(new HttpSuccess(204, {}));
}

async function del(req, res, next) {
    assert.strictEqual(typeof req.resource, 'object');

    const [error] = await safe(dockerRegistries.del(req.resource, AuditSource.fromRequest(req)));
    if (error) return next(BoxError.toHttpError(error));

    next(new HttpSuccess(204));
}

export default {
    list,
    add,
    get,
    del,
    update,
    load
};
