"""Registre de prévention, DUERP et suivi du temps dédié."""
from datetime import datetime, timezone
from app_new.extensions import db


class PreventionWorkLog(db.Model):
    __tablename__ = "prevention_work_logs"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False, index=True)
    work_date = db.Column(db.Date, nullable=False, index=True)
    duration_minutes = db.Column(db.Integer, nullable=False)
    activity = db.Column(db.String(255), nullable=False)
    notes = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
    user = db.relationship("User")


class StaffAuthorization(db.Model):
    __tablename__ = "staff_authorizations"
    id = db.Column(db.Integer, primary_key=True)
    staff_id = db.Column(db.Integer, db.ForeignKey("staff.id"), nullable=False, index=True)
    name = db.Column(db.String(160), nullable=False)
    issued_on = db.Column(db.Date)
    expires_on = db.Column(db.Date, index=True)
    issuer = db.Column(db.String(160))
    notes = db.Column(db.Text)
    staff = db.relationship("Staff", backref=db.backref("authorizations", cascade="all, delete-orphan"))


class RiskAssessment(db.Model):
    __tablename__ = "risk_assessments"
    id = db.Column(db.Integer, primary_key=True)
    work_unit = db.Column(db.String(160), nullable=False)
    hazard = db.Column(db.String(255), nullable=False)
    exposed_people = db.Column(db.String(255))
    severity = db.Column(db.Integer, nullable=False, default=1)
    probability = db.Column(db.Integer, nullable=False, default=1)
    control_measures = db.Column(db.Text)
    reviewed_on = db.Column(db.Date)
    is_active = db.Column(db.Boolean, nullable=False, default=True)
    @property
    def score(self): return self.severity * self.probability


class PreventionAction(db.Model):
    __tablename__ = "prevention_actions"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)
    source_type = db.Column(db.String(40), nullable=False, default="observation")
    risk_id = db.Column(db.Integer, db.ForeignKey("risk_assessments.id"))
    priority = db.Column(db.String(20), nullable=False, default="normale")
    owner = db.Column(db.String(160))
    due_date = db.Column(db.Date, index=True)
    status = db.Column(db.String(30), nullable=False, default="a_faire")
    evidence = db.Column(db.Text)
    risk = db.relationship("RiskAssessment", backref="actions")


class SafetyRegisterEntry(db.Model):
    __tablename__ = "safety_register_entries"
    id = db.Column(db.Integer, primary_key=True)
    entry_date = db.Column(db.Date, nullable=False, index=True)
    entry_type = db.Column(db.String(50), nullable=False)
    title = db.Column(db.String(255), nullable=False)
    details = db.Column(db.Text)
    location = db.Column(db.String(255))
    follow_up = db.Column(db.Text)
    created_by_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    created_by = db.relationship("User")
