import csv
import datetime
import os

from django.conf import settings
from django.core.management.base import BaseCommand

from ...models import Diary


class Command(BaseCommand):
    help = "Backup Diary Data"

    def handle(self,*args,**kwargs):
        date = datetime.date.today().strftime("%Y%m%d")

        file_path = settings.BACKUP_PATH+'diary'+date+'.csv'
        os.makedirs(settings.BACKUP_PATH, exist_ok= True)

        with open(file_path,'w')as file:
            writter = csv.writer(file)

            header = [field.name for field in Diary._meta.fields]
            writter.writerow(header)

            diaries = Diary.objects.all()

            for diary in diaries:
                writter.writerow([str(diary.user),
                                  diary.title,
                                  diary.content,
                                  str(diary.photo1),
                                  str(diary.photo2),
                                  str(diary.photo3),
                                  str(diary.created_at),
                                  str(diary.updated_at)])


            files = os.listdir(settings.BACKUP_PATH)

            if len(files) >= settings.NUM_SAVED_BACKUP:
                files.sort()
                os.remove(settings.BACKUP_PATH+files[0])