from sqlalchemy import Column, BigInteger, String, Float, Boolean, TIMESTAMP, ForeignKey, Enum
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql import func
from backend.app.db.models.portfolio import Portfolio, PropertyModel
from backend.app.db.models.materials import Material
from datatypes.enums import QuantityUnits

Base = declarative_base()


class Recommendation(Base):
    __tablename__ = 'recommendation'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    property_id = Column(BigInteger, ForeignKey(PropertyModel.id), nullable=False)
    created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
    type = Column(String, nullable=False)
    description = Column(String, nullable=False)
    estimated_cost = Column(Float)
    default = Column(Boolean, nullable=False)
    starting_u_value = Column(Float)
    new_u_value = Column(Float)
    sap_points = Column(Float)
    heat_demand = Column(Float)
    co2_equivalent_savings = Column(Float)
    energy_savings = Column(Float)
    energy_cost_savings = Column(Float)
    property_valuation_increase = Column(Float)
    rental_yield_increase = Column(Float)
    total_work_hours = Column(Float)
    labour_days = Column(Float)


class RecommendationMaterials(Base):
    __tablename__ = 'recommendation_materials'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    recommendation_id = Column(BigInteger, ForeignKey('recommendation.id'), nullable=False)
    material_id = Column(BigInteger, ForeignKey(Material.id), nullable=False)
    created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
    depth = Column(Float, nullable=False)
    quantity = Column(Float, nullable=False)
    quantity_unit = Column(Enum(QuantityUnits, values_callable=lambda x: [e.value for e in x]), nullable=False)
    estimated_cost = Column(Float, nullable=False)


class Plan(Base):
    __tablename__ = 'plan'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    portfolio_id = Column(BigInteger, ForeignKey(Portfolio.id), nullable=False)
    property_id = Column(BigInteger, ForeignKey(PropertyModel.id), nullable=False)
    created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
    is_default = Column(Boolean, nullable=False)


class PlanRecommendations(Base):
    __tablename__ = 'plan_recommendations'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    plan_id = Column(BigInteger, ForeignKey('plan.id'), nullable=False)
    recommendation_id = Column(BigInteger, ForeignKey('recommendation.id'), nullable=False)
