import {Controller, http, HTTPStatus, Inject, Injectable, Logging, view} from '@extollo/lib'
import {Snippet} from '../../models/Snippet.model'
import {Home} from './Home.controller'

/**
 * Snippets Controller
 * ------------------------------------
 * Backend for routes that deal with code snippets.
 */
@Injectable()
export class Snippets extends Controller {
    @Inject()
    protected readonly logging!: Logging

    public async viewSnippet(snippet: Snippet) {
        const needsAccessKey = snippet?.accessKey && snippet.accessKey !== this.request.input('accessKey')
        const needsConfirm = snippet?.singleAccessOnly && !this.request.input('confirmSingleAccess')

        if ( snippet?.singleAccessOnly && !needsAccessKey && !needsConfirm ) {
            await snippet.delete()
        }

        const home = <Home> this.make(Home)

        return view('snippet', {
            snippet,
            needsAccessKey,
            needsConfirm,
            accessKey: this.request.input('accessKey'),
            ...home.getThemeCSS(),
        })
    }
}
