import multer from "multer";
import shortid from "shortid";
import path from 'path'
import { config } from "../index.config";
import express, { Request, Response, Router } from "express";
import { PhotosService, photosService } from "../services/photosService";
import IModifiedRequest from "../interfaces/IModifiedRequest";
import IPhotoDto from "../interfaces/IPhotoDto";
import { EStatuses } from "../enum/EStatuses";
import { auth } from "../middlewares/auth";

const storage = multer.diskStorage({
    destination(req, file, callback){
        callback(null, config.filePath)
    },
    filename(req, file, callback){
        callback(null, shortid.generate() + path.extname(file.originalname))
    },
})

const upload = multer({storage})

export class PhotosController {
    private router: Router
    private service: PhotosService
    constructor(){
        this.router = express.Router()
        this.service = photosService
        this.router.get('/', this.getAllPhotos)
        this.router.get('/:id', this.getPhotosByUserId)
        this.router.post('/', [auth, upload.single('photo')], this.addPhoto)
        this.router.delete('/:id',auth, this.deletePhotoById)
    }
    public getRouter() {
        return this.router;
    }
    private getAllPhotos = async(req: Request, res: Response): Promise<void> => {
        const response = await this.service.getAllPhotos()
        if (response.status === EStatuses.FAILURE){
            res.status(418).send(response)
        } else{
            res.status(200).send(response)
        }
    }

    private addPhoto = async(req: Request, res: Response): Promise<void> => {
        const modifiedReq = req as IModifiedRequest
        if (typeof modifiedReq.verifiedData === 'object'){
            const photoDto: IPhotoDto = {
                user: modifiedReq.verifiedData._id,
                title: req.body.title,
                photo: req.file ? req.file.filename : '',
            }
            const response = await this.service.addPhoto(photoDto)
            if (response.status === EStatuses.FAILURE){
                res.status(418).send(response)
            } else{
                res.status(200).send(response)
            }
        } else{
            res.status(418).send('error in request. Some invalid field appeared')
        }
    }

    private getPhotosByUserId = async (req: Request, res: Response): Promise<void> => {
        const response = await this.service.getPhotosByUserId(req.params.id)
        response.status === EStatuses.FAILURE ? res.status(418).send(response) : res.status(200).send(response)
    }

    private deletePhotoById = async (req: Request, res: Response): Promise<void> => {
        const modifiedReq = req as IModifiedRequest
        if (typeof modifiedReq.verifiedData === 'object'){
            const response = await this.service.deletePhotoById(req.params.id, modifiedReq.verifiedData._id)
            if (response.status === EStatuses.FAILURE){
                res.status(418).send(response)
            } else{
                res.status(200).send(response)
            }
        } else{
            res.status(418).send('error in request. Some invalid field appeared')
        }
    }
}

export const photosController = new PhotosController()