from app.extensions import db
from datetime import datetime, timezone

# Create a model for Volunteer Applications
class VolunteerApplication(db.Model):

    # Set the actual table name in the database
    __tablename__ = 'volunteer_applications'

    # Primary Key: Unique ID for every application
    id = db.Column(db.Integer, primary_key=True)

    # A unique reference number given to the applicant to track their form status
    reference_id = db.Column(db.String(50), unique=True, nullable=False)

    @property
    def reference_no(self):
        """Property alias so reference_no and reference_id can be accessed interchangeably."""
        return self.reference_id

    @property
    def full_name(self):
        """Property alias so full_name and name can be accessed interchangeably."""
        return self.name

    def __repr__(self) -> str:
        """String representation of VolunteerApplication."""
        return f"<VolunteerApplication {self.reference_id} ({self.name})>"


    # Links to the users table (nullable=True means even non-logged-in users might apply, depending on rules)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
    assigned_to = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True, index=True)

    # Applicant details: Name, State, and District
    name = db.Column(db.String(100), nullable=False)
    state = db.Column(db.String(50), nullable = False)
    district = db.Column(db.String(50), nullable = False)

    # What skills the volunteer has and their availability time
    skills = db.Column(db.Text, nullable=True)
    availability = db.Column(db.String(100), nullable=True)

    # Application Status (e.g., 'Submitted', 'Under Review', 'Approved', 'Rejected')
    status = db.Column(db.String(50), default='Submitted')

    # Timestamp of when the form was submitted
    submitted_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))