package relations.m2mattr;

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

/**
 * ============================================================================
 * 关系情形 ③：多对多【带属性】—— Employee（本情形与 VolunteerHub 主案例同构）
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — An employee works on many projects and a project has many employees, but the link
 *      also stores role and hoursPerWeek. Therefore the link is an entity: Assignment.
 *      This side has @OneToMany(mappedBy = "employee").
 * 中 — 一个员工参与多个项目，一个项目有多个员工，但关联本身还要记录角色与每周工时，
 *      因此关联必须是一个实体：Assignment。本端用 @OneToMany(mappedBy = "employee")。
 *
 * 【生成表 / Generated tables】
 *   employee(id, name)
 *   project(id, name)
 *   assignment(id, employee_id, project_id, role, hours_per_week)   ← 多出来的两列
 *
 * 【考试判断 / Exam cue】
 * EN — "many … many" PLUS date / quantity / role / rating → you MUST split it into an
 *      intermediate entity. @ManyToMany could not store those columns at all.
 * 中 — “多对多”再加日期/数量/角色/评分 → 必须拆成中间实体；@ManyToMany 根本放不下这些列。
 */
@Entity
public class Employee {

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

    private String name;

    @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Assignment> assignments = new ArrayList<>();

    public Employee() { }
    public Employee(String name) { this.name = name; }

    public void addAssignment(Assignment a) {
        assignments.add(a);
        a.setEmployee(this);
    }

    public Long getId() { return id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public List<Assignment> getAssignments() { return assignments; }
}
