from repository import Repository
from reservation import Reservation
import random
from datetime import date,timedelta

class Controller:
    def __init__(self,repo) -> None:
        self.repo=repo
    def display(self,start,end):
        return sorted(sorted(self.repo.get_by_date(start,end),key=lambda x:x.last_name),key=lambda x:x.start_date)
    def get_empty_rooms(self,start,end):
        """
        :params:
            start: date
            end: date
        :return:
            list of empty rooms
        
        Function that returns a list of empty rooms for a given period of time
        """
        return self.repo.get_empty_rooms(start,end)
    def create_reservation(self,last_name,first_name,room_type,guests,start,end):
        """
        :params:
            last_name: string
            first_name: string
            room_type: string
            guests: string
            start: date
            end: date
        :return:
            None
        
        Function that creates a reservation. The id is generated randomly and the reservation is added to the repository
        """
        new_reservation=Reservation(random.randint(1000,9999),last_name,first_name,room_type,guests,start,end)
        self.repo.add_reservation(new_reservation)
        pass
    def delete_reservation(self,id):
        self.repo.delete_reservation(id)
    def delete_reservation_by_date(self,start,end,room):
        res = sorted(sorted(self.repo.get_by_date(start,end),key=lambda x:x.last_name),key=lambda x:x.start_date)
        res_returned = []
        for i in res:
            if i.room_type == room:
                self.repo.delete_reservation(i.id)
                res_returned.append(i)
        return res_returned

        pass
    def monthly_report(self,month):
        weeks=[[] for i in range(6)]
        days=[None for i in range(1,(date(2023,month+1,1)-date(2023,month,1)).days+1)]
        for i in range(1,(date(2023,month+1,1)-date(2023,month,1)).days+1):
            days[i-1] = len(self.repo.get_empty_rooms(date(2023,month,i),date(2023,month,i)+timedelta(1)))
        week=0
        for i in range(date(2023,month,1).weekday()):
            weeks[week].append("x")
        for i in range(1,(date(2023,month+1,1)-date(2023,month,1)).days+1):
            weeks[week].append(str(i)+"/"+str(days[i-1]))
            if (i+date(2023,month,1).weekday())%7==0:
                week+=1
        while len(weeks[week])<7:
            weeks[week].append("x")
        return weeks

