/*
 * Copyright 2014-2023 michael-simons.eu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ac.simons.biking2.tracks;

import ac.simons.biking2.config.DatastoreConfig;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.io.File;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author Michael J. Simons, 2014-02-08
 */
@Entity
@Table(name = "tracks", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"covered_on", "name"})
})
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyOrder(alphabetic = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@NoArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@EqualsAndHashCode(of = {"name", "coveredOn"})
class TrackEntity implements Serializable {

    private static final long serialVersionUID = 7630613853916630933L;

    public enum Type {

        biking, running
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Integer id;

    @Column(length = 512, nullable = false)
    @NotBlank
    @Size(max = 512)
    private String name;

    @Column(name = "covered_on", nullable = false)
    @NotNull
    private LocalDate coveredOn;

    @Column(length = 2048)
    @Setter
    private String description;

    @Column(precision = 18, scale = 15)
    @Setter
    private BigDecimal minlat;

    @Column(precision = 18, scale = 15)
    @Setter
    private BigDecimal minlon;

    @Column(precision = 18, scale = 15)
    @Setter
    private BigDecimal maxlat;

    @Column(precision = 18, scale = 15)
    @Setter
    private BigDecimal maxlon;

    @Enumerated(EnumType.STRING)
    @Setter
    private Type type = Type.biking;

    TrackEntity(final String name, final LocalDate coveredOn) {
        this.name = name;
        this.coveredOn = coveredOn;
    }

    @JsonProperty("id")
    public String getPrettyId() {
        return this.getId() == null ? "" : Integer.toString(this.getId(), 36);
    }

    public File getTrackFile(final File datastoreBaseDirectory, final String format) {
        return new File(datastoreBaseDirectory, String.format("%s/%d.%s", DatastoreConfig.TRACK_DIRECTORY, this.getId(), format));
    }
}
