class Model(db.Model):
    __tablename__ = "model"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True, nullable=False)
    description = db.Column(db.Text, unique=False, nullable=False)
    created_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    created_by = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False)

    def __repr__(self):
        return f"Model|'{self.name}'),'{self.description}','{self.created_date}'"
class Project(db.Model):
    __tablename__ = "project"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True, nullable=False)
    description = db.Column(db.Text, unique=False, nullable=False)
    created_date = db.Column(
        db.DateTime, nullable=False, default=datetime.utcnow
    )  # auto
    created_by = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False)
    program = db.relationship("Program", back_populates="project")
    case = db.relationship("Case", back_populates="project")  # forward
    def __repr__(self):
        return f"Program|'{self.name}'),'{self.description}','{self.created_date}','{self.created_by}', '{self.program_id}','{self.program}'"
class Part(db.Model):
    __tablename__ = "part"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True, nullable=False)
    description = db.Column(db.Text, unique=False, nullable=False)
    created_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    created_by = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
class RuntimeConfig(db.Model):
    __tablename__ = "runtime_config"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True, nullable=False)
    description = db.Column(db.Text, unique=False, nullable=False)
    created_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    program_id = db.Column(db.Integer, db.ForeignKey("program.id"), nullable=False)
class Case(db.Model):
    __tablename__ = "case"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=False, nullable=False)
    description = db.Column(db.Text, unique=False, nullable=False)
    created_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    created_by = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
    project_id = db.Column(db.Integer, db.ForeignKey("project.id"), nullable=False)
    model_id = db.Column(db.Integer, db.ForeignKey("model.id"), nullable=False)
    baseline_id = db.Column(db.Integer, ForeignKey("model.id"))
    ridemap_id = db.Column(db.Integer, db.ForeignKey("ridemap.id"), nullable=False)

    project = db.relationship("Project", back_populates="case")

    geometry_id = db.Column(db.Integer, ForeignKey("geomconfig.id"))
    

    def __repr__(self):
        return f"Case|('{self.id}','{self.name}'),'{self.description}','{self.project_id}','{self.model_id}'"
        return f"Model|'{self.name}'),'{self.description}','{self.created_date}'"


class Part(db.Model):
    __tablename__ = "part"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True, nullable=False)
    description = db.Column(db.Text, unique=False, nullable=False)
    created_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    created_by = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)


Model_Part = db.Table(
    "Model_Part",
    db.Model.metadata,
    db.Column("model_id", db.Integer, db.ForeignKey("model.id"), primary_key=True),
    db.Column("part_id", db.Integer, db.ForeignKey("part.id"), primary_key=True),
)


class RideHeight(db.Model):
    __tablename__ = "rideheight"
    id = db.Column(db.Integer, primary_key=True)
    frh = db.Column(db.Float(), nullable=False)
    rrh = db.Column(db.Float(), nullable=False)
    yaw = db.Column(db.Float(), nullable=False)
    roll = db.Column(db.Float(), nullable=False)

    ridemap = db.relationship(
        "RideMap",
        secondary="RideMap_Rideheight",