from app.extensions import db
from datetime import datetime, timezone

# Create a model for people requesting support/help from the NGO
class SupportRequest(db.Model):
    # Set the actual table name in the database
    __tablename__ = 'support_requests'

    # Primary Key: Unique ID for every support request
    id = db.Column(db.Integer, primary_key=True)
    
    # A unique tracking number for the applicant to check their status
    reference_no = db.Column(db.String(50), unique=True, nullable=False)
    
    # Category of support needed (e.g., education fees, health, ration, shelter)
    category = db.Column(db.String(100), nullable=False)
    
    # Applicant's contact and location details
    contact_details = db.Column(db.String(255), nullable=False)
    location = db.Column(db.String(100), nullable=False)
    
    # The actual request message or explanation of their problem
    request_text = db.Column(db.Text, nullable=False)
    
    # Status of the request (e.g., 'Submitted', 'Under Review', 'Approved')
    status = db.Column(db.String(50), default='Submitted')
    
    # Automatically log when the request was made
    submitted_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
    
    # Foreign Key: Which staff member (User ID) is handling this request
    assigned_to = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)

    @property
    def reference_id(self) -> str:
        """Alias reference_id to match VolunteerApplication convention."""
        return self.reference_no

    @property
    def name(self) -> str:
        """Extract applicant name from contact_details string."""
        if not self.contact_details:
            return "Anonymous Requester"
        parts = [p.strip() for p in self.contact_details.split('|') if p.strip()]
        return parts[0] if parts else "Anonymous Requester"

    @property
    def full_name(self) -> str:
        """Alias full_name to name property."""
        return self.name

    def __repr__(self) -> str:
        """String representation of SupportRequest."""
        return f"<SupportRequest {self.reference_no} ({self.category})>"