package com.example.volunteer.model;

import jakarta.persistence.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * ============================================================================
 * (a)(d) 实体模型 / Entity model — Event（志愿活动）
 * ============================================================================
 *
 * 【题目 / Question (a)】
 * EN — Design and write the Java code for the entity models – Volunteer, Event and Signup.
 *      You may need to add more attributes and methods wherever necessary.
 * 中 — 设计并写出实体模型 Volunteer、Event、Signup；可按需增加属性与方法。
 *
 * 【题目 / Question (d)】
 * EN — The page must display, for each event: title, date, location, capacity, number of
 *      sign-ups, total hours and the fill rate as a percentage.
 * 中 — 页面要显示每个活动的：标题、日期、地点、容量、报名人数、总服务时数、满员率（百分比）。
 *      → 因此本类提供 getSignupCount()、getTotalHours()、getFillRate() 三个计算属性。
 *
 * 【关系 / Relationship】
 * EN — One event has many sign-ups. cascade = ALL because a sign-up has no meaning without
 *      its event; orphanRemoval = true so that removing a sign-up from the list deletes the row.
 * 中 — 一个活动有多条报名；cascade = ALL 因为报名离开活动没有意义；orphanRemoval = true
 *      表示从集合里移除即删除该行。
 *
 * 生成表 / Generated table:
 *   event(id, title, event_date, location, capacity)
 */
@Entity
public class Event {

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

    private String title;
    private LocalDate eventDate;
    private String location;
    private int capacity;

    @OneToMany(mappedBy = "event", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Signup> signups = new ArrayList<>();

    public Event() { }

    public Event(String title, LocalDate eventDate, String location, int capacity) {
        this.title = title;
        this.eventDate = eventDate;
        this.location = location;
        this.capacity = capacity;
    }

    /** 双向一致性：往集合里加的同时，设置对方的 event 字段（否则外键为 null）。 */
    public void addSignup(Signup signup) {
        signups.add(signup);
        signup.setEvent(this);
    }

    public void removeSignup(Signup signup) {
        signups.remove(signup);
        signup.setEvent(null);
    }

    // ------------------------------------------------------- 计算属性 / derived properties（不落库）

    /** 报名人数 / number of sign-ups */
    public int getSignupCount() {
        return signups.size();
    }

    /** 总服务时数 / total contributed hours */
    public int getTotalHours() {
        int total = 0;
        for (Signup s : signups) {
            total += s.getHours();
        }
        return total;
    }

    /** 满员率 0..1 / fill rate; 0 表示 capacity 未填时避免除零 */
    public double getFillRate() {
        return capacity == 0 ? 0.0 : (double) signups.size() / capacity;
    }

    // ------------------------------------------------------------------ getters / setters

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public LocalDate getEventDate() { return eventDate; }
    public void setEventDate(LocalDate eventDate) { this.eventDate = eventDate; }

    public String getLocation() { return location; }
    public void setLocation(String location) { this.location = location; }

    public int getCapacity() { return capacity; }
    public void setCapacity(int capacity) { this.capacity = capacity; }

    public List<Signup> getSignups() { return signups; }
    public void setSignups(List<Signup> signups) { this.signups = signups; }
}
