package mx.gob.jumapacelaya.models;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class Ticket {
    private final int id;
    private final String subject;
    private final String description;
    private final String status;
    private final LocalDateTime dateCreate;
    private LocalDateTime dateClose;
    private final LocalDateTime updateOn;
    private User autor;
    private Integer trackerId;
    private String type;
    private Double estimatedHrs;


    public Ticket(int id, String subject, String description, String status,
                  String dateCreate, String dateClose, String updateOn,
                  User autor, Integer trackerId, String type, Double estimatedHrs) {
        this.id = id;
        this.subject = subject;
        this.description = description;
        this.status = status;

        this.dateCreate = parseDate(dateCreate);
        this.dateClose = parseDate(dateClose);
        this.updateOn = parseDate(updateOn);

        this.autor = autor;
        this.trackerId = trackerId;
        this.type = type;
        this.estimatedHrs = estimatedHrs;
    }


    /**
     * Método para parsear fechas en formato:
     * - yyyy-MM-dd
     * - yyyy-MM-dd'T'HH:mm:ssZ
     * Siempre convierte a zona horaria local del sistema.
     */
    private static LocalDateTime parseDate(String dateStr) {
        if (dateStr == null || dateStr.isEmpty()) {
            return null;
        }

        List<DateTimeFormatter> formatters = List.of(
                DateTimeFormatter.ISO_OFFSET_DATE_TIME,
                DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"),
                DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm")
        );

        for (DateTimeFormatter formatter : formatters) {
            try {
                return LocalDateTime.parse(dateStr, formatter);
            } catch (Exception ignored) {}
        }

        try {
            LocalDate ld = LocalDate.parse(dateStr);
            return ld.atStartOfDay();
        } catch (Exception e) {
            System.err.println("Error al parsear fecha: " + dateStr);
            return null;
        }
    }


    public int getId() {
        return id;
    }

    public String getSubject() {
        return subject;
    }

    public String getDescription() {
        return description;
    }

    public String getStatus() {
        return status;
    }

    public User getAuthor() {
        return autor;
    }

    public void setAuthor(User author) {
        this.autor = author;
    }

    public LocalDateTime getDateCreate() {
        return dateCreate;
    }

    public Integer getTrackerId() {
        return trackerId;
    }

    public void setTrackerId(Integer tipoId) {
        this.trackerId = tipoId;
    }

    public LocalDateTime getDateClose() {
        return dateClose;
    }

    public void setDateClose(LocalDateTime dateClose) {
        this.dateClose = dateClose;
    }

    public void setType(String type) {
        this.type = type;
    }

    public LocalDateTime getUpdateOn() {
        return updateOn;
    }

    public LocalDateTime getDateCreateLocal() {
        return this.dateCreate;
    }

    public LocalDateTime getDateCloseLocal() {
        return this.dateClose;
    }

    public Double getEstimatedHrs() {
        return estimatedHrs;
    }

    public void setEstimatedHrs(Double estimatedHrs) {
        this.estimatedHrs = estimatedHrs;
    }


    public static class User {
        private String username;

        public User(String username) {
            this.username = username;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
    }


    public String tiempoEst(Integer trackerId) {
        if (trackerId == null) {
            return "Desconocido";
        }

        switch (trackerId) {
            case 5:
                return "12 horas";
            case 6,16:
                return "2 horas";
            case 7,8,12,13:
                return "48 horas";
            case 9,10:
                return "72 horas";
            case 11,14,15:
                return "24 horas";
            default:
                return "N/A";
        }
    }


    public String getType() {
        if (trackerId == null) {
            return "Desconocido";
        }
        return switch (trackerId) {
            case 5 -> "Acceso/Permiso/Bajas";
            case 6 -> "Soporte de Software";
            case 7 -> "Capacitacion de Software";
            case 8 -> "Configuracion de Software";
            case 9 -> "Desarrollo de Software";
            case 10 -> "Digitalizacion GIS";
            case 11 -> "Documento";
            case 12 -> "Reporte";
            case 13 -> "Mantenimiento Correctivo";
            case 14 -> "Actividad";
            case 15 -> "Entrega de Consumibles";
            case 16 -> "Instalacion/Configuracion GIS";
            //case 17 -> "DDL";
            //case 18 -> "DCL";
            //case 19 -> "DML";
            default -> "N/A";
        };
    }
}
