package com.example.pap_teste.models;

import java.io.Serializable;

public class Restaurant implements Serializable {
    private String name;
    private String category;
    private String email;
    private boolean available;
    private String logoUrl;
    private Double ratingAvg;
    private Integer ratingCount;
    private boolean fechadoWebsite;

    // No-argument constructor required for Firebase
    public Restaurant() {
    }

    public Restaurant(String name, String category, String email, boolean available) {
        this.name = name;
        this.category = category;
        this.email = email;
        this.available = available;
    }

    public Restaurant(String name, String category, String email, boolean available, String logoUrl) {
        this.name = name;
        this.category = category;
        this.email = email;
        this.available = available;
        this.logoUrl = logoUrl;
    }

    public String getName() { return name; }
    public String getCategory() { return category; }
    public String getEmail() { return email; }
    public boolean isAvailable() { return available; }
    public String getLogoUrl() { return logoUrl; }

    public void setName(String name) { this.name = name; }
    public void setCategory(String category) { this.category = category; }
    public void setEmail(String email) { this.email = email; }
    public void setAvailable(boolean available) { this.available = available; }
    public void setLogoUrl(String logoUrl) { this.logoUrl = logoUrl; }
    public boolean isFechadoWebsite() { return fechadoWebsite; }
    public void setFechadoWebsite(boolean fechadoWebsite) { this.fechadoWebsite = fechadoWebsite; }
    
    public Double getRatingAvg() { return ratingAvg; }
    public void setRatingAvg(Double ratingAvg) { this.ratingAvg = ratingAvg; }
    public Integer getRatingCount() { return ratingCount; }
    public void setRatingCount(Integer ratingCount) { this.ratingCount = ratingCount; }
    
    private java.util.Map<String, ScheduleDay> schedule;
    public java.util.Map<String, ScheduleDay> getSchedule() { return schedule; }
    public void setSchedule(java.util.Map<String, ScheduleDay> schedule) { this.schedule = schedule; }

    public ScheduleDay getScheduleForDay(int dayOfWeekCalendar) {
        if (schedule == null) return null;
        String[] dayKeys = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
        String[] dayKeysPt = {"domingo", "segunda-feira", "terça-feira", "quarta-feira", "quinta-feira", "sexta-feira", "sábado"};
        String[] dayKeysPtShort = {"domingo", "segunda", "terça", "quarta", "quinta", "sexta", "sábado"};
        int index = dayOfWeekCalendar - 1;
        
        ScheduleDay sd = schedule.get(dayKeys[index]);
        if (sd == null) sd = schedule.get(dayKeysPt[index]);
        if (sd == null) sd = schedule.get(dayKeysPtShort[index]);
        if (sd == null) sd = schedule.get(String.valueOf(index));
        
        if (sd == null) {
            for (java.util.Map.Entry<String, ScheduleDay> entry : schedule.entrySet()) {
                String k = entry.getKey().toLowerCase(java.util.Locale.ROOT).replace("-", "").replace(" ", "");
                String expected1 = dayKeys[index].replace("-", "");
                String expected2 = dayKeysPt[index].replace("-", "");
                String expected3 = dayKeysPtShort[index].replace("-", "");
                
                if (k.equals(expected1) || k.equals(expected2) || k.equals(expected3) || k.equals(String.valueOf(index))) {
                    return entry.getValue();
                }
            }
        }
        return sd;
    }

    public boolean isClosedForDay(int dayOfWeekCalendar) {
        if (schedule == null || schedule.isEmpty()) return false; // If no schedule is set at all, default to open to avoid locking out old restaurants.
        ScheduleDay sd = getScheduleForDay(dayOfWeekCalendar);
        if (sd == null) {
            return true; // Se o dia não existe no mapa de um restaurante com horário, está fechado.
        }
        return sd.isClosed();
    }
}
