package relations.m2mattr;

import jakarta.persistence.*;

/**
 * ============================================================================
 * 关系情形 ③：中间实体（关联类 / association class）—— Assignment
 * ============================================================================
 *
 * 【本题对照 / How this relates to VolunteerHub】
 * EN — This is exactly the role played by Signup in the main project:
 *      Employee ~ Volunteer, Project ~ Event, Assignment ~ Signup.
 * 中 — 它在结构上就是主工程里的 Signup：Employee ≈ Volunteer，Project ≈ Event，Assignment ≈ Signup。
 *
 * 【为什么需要自己的 id / Why it has its own id】
 * EN — With its own primary key you can address one sign-up record directly, e.g.
 *      DELETE /api/assignments/{id} or "delete this one sign-up". A plain join table has
 *      only a composite key and cannot be addressed as a row.
 * 中 — 有了自己的主键，才能直接操作某一条记录，例如 DELETE /api/assignments/{id}。
 *      纯中间表只有联合主键，不能作为一行被单独寻址。
 */
@Entity
public class Assignment {

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

    @ManyToOne(optional = false)
    @JoinColumn(name = "employee_id", nullable = false)
    private Employee employee;

    @ManyToOne(optional = false)
    @JoinColumn(name = "project_id", nullable = false)
    private Project project;

    /** 关联上的业务属性 / attributes that belong to the relationship itself */
    private String role;
    private int hoursPerWeek;

    public Assignment() { }
    public Assignment(String role, int hoursPerWeek) {
        this.role = role;
        this.hoursPerWeek = hoursPerWeek;
    }

    public Long getId() { return id; }
    public Employee getEmployee() { return employee; }
    public void setEmployee(Employee employee) { this.employee = employee; }
    public Project getProject() { return project; }
    public void setProject(Project project) { this.project = project; }
    public String getRole() { return role; }
    public void setRole(String role) { this.role = role; }
    public int getHoursPerWeek() { return hoursPerWeek; }
    public void setHoursPerWeek(int hoursPerWeek) { this.hoursPerWeek = hoursPerWeek; }
}
