import Project from '#models/project';
import Category from '#models/category';
import Tag from '#models/tag';
import Training from '#models/training';
import Image from '#models/image';
import Experience from '#models/experience';
import { DateTime } from 'luxon';
export default class ProjectsController {
    async index({ view }) {
        const projects = await Project.query().preload('category').orderBy('start', 'asc');
        return view.render('admin/projects/index', { projects });
    }
    async create({ view }) {
        const [categories, tags, trainings, images, experiences] = await Promise.all([
            Category.all(),
            Tag.query().orderBy('name', 'asc'),
            Training.all(),
            Image.all(),
            Experience.all(),
        ]);
        return view.render('admin/projects/form', {
            project: null,
            categories,
            tags,
            trainings,
            images,
            experiences,
        });
    }
    async store({ request, response }) {
        const body = request.only([
            'title',
            'content',
            'start',
            'end',
            'thumbnailUrl',
            'categoryId',
            'tagIds',
            'trainingIds',
            'experienceIds',
        ]);
        let start = null;
        if (body.start) {
            const parsed = DateTime.fromISO(body.start);
            if (parsed.isValid)
                start = parsed;
        }
        let end = null;
        if (body.end) {
            const parsed = DateTime.fromISO(body.end);
            if (parsed.isValid)
                end = parsed;
        }
        const project = await Project.create({
            title: body.title,
            content: body.content || null,
            start,
            end,
            thumbnailUrl: body.thumbnailUrl || null,
            categoryId: body.categoryId ? Number(body.categoryId) : null,
        });
        const tagIds = Array.isArray(body.tagIds) ? body.tagIds : body.tagIds ? [body.tagIds] : [];
        const trainingIds = Array.isArray(body.trainingIds)
            ? body.trainingIds
            : body.trainingIds
                ? [body.trainingIds]
                : [];
        const experienceIds = Array.isArray(body.experienceIds)
            ? body.experienceIds
            : body.experienceIds
                ? [body.experienceIds]
                : [];
        if (tagIds.length)
            await project.related('tags').attach(tagIds.map(Number));
        if (trainingIds.length)
            await project.related('trainings').attach(trainingIds.map(Number));
        if (experienceIds.length)
            await project.related('experiences').attach(experienceIds.map(Number));
        return response.redirect('/admin/projects');
    }
    async edit({ view, params, response }) {
        const project = await Project.query()
            .where('id', params.id)
            .preload('tags')
            .preload('trainings')
            .preload('experiences')
            .first();
        if (!project) {
            return response.notFound();
        }
        const [categories, tags, trainings, images, experiences] = await Promise.all([
            Category.all(),
            Tag.query().orderBy('name', 'asc'),
            Training.all(),
            Image.all(),
            Experience.all(),
        ]);
        return view.render('admin/projects/form', {
            project,
            categories,
            tags,
            trainings,
            images,
            experiences,
        });
    }
    async update({ request, params, response }) {
        const project = await Project.find(params.id);
        if (!project) {
            return response.notFound();
        }
        const body = request.only([
            'title',
            'content',
            'start',
            'end',
            'thumbnailUrl',
            'categoryId',
            'tagIds',
            'trainingIds',
            'experienceIds',
        ]);
        let start = null;
        if (body.start) {
            const parsed = DateTime.fromISO(body.start);
            if (parsed.isValid)
                start = parsed;
        }
        let end = null;
        if (body.end) {
            const parsed = DateTime.fromISO(body.end);
            if (parsed.isValid)
                end = parsed;
        }
        await project
            .merge({
            title: body.title,
            content: body.content || null,
            start,
            end,
            thumbnailUrl: body.thumbnailUrl || null,
            categoryId: body.categoryId ? Number(body.categoryId) : null,
        })
            .save();
        const tagIds = Array.isArray(body.tagIds) ? body.tagIds : body.tagIds ? [body.tagIds] : [];
        const trainingIds = Array.isArray(body.trainingIds)
            ? body.trainingIds
            : body.trainingIds
                ? [body.trainingIds]
                : [];
        const experienceIds = Array.isArray(body.experienceIds)
            ? body.experienceIds
            : body.experienceIds
                ? [body.experienceIds]
                : [];
        await project.related('tags').sync(tagIds.map(Number));
        await project.related('trainings').sync(trainingIds.map(Number));
        await project.related('experiences').sync(experienceIds.map(Number));
        return response.redirect('/admin/projects');
    }
    async destroy({ params, response }) {
        const project = await Project.find(params.id);
        if (!project) {
            return response.notFound();
        }
        await project.delete();
        return response.redirect('/admin/projects');
    }
}
//# sourceMappingURL=projects_controller.js.map