{% extends 'base.html' %}
{% load static %}

{% block title %}User Profile - RaktaPulse{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="glass-card">
                <div class="d-flex align-items-center mb-4">
                    {% if user.profile.profile_pic %}
                        <img src="{{ user.profile.profile_pic.url }}" alt="{{ user.username }}" class="rounded-circle me-3 object-fit-cover" style="width: 64px; height: 64px;">
                    {% else %}
                        <div class="bg-danger rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 60px; height: 60px;">
                            <i class="bi bi-person-fill text-white fs-2"></i>
                        </div>
                    {% endif %}
                    <div>
                        <h2 class="mb-0 fw-bold">{{ user.username }}</h2>
                        <p class="text-secondary mb-0">Member since {{ user.date_joined|date:"M d, Y" }}</p>
                    </div>
                </div>

                <form method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                    <h5 class="fw-bold mb-3 border-bottom pb-2">Account Information</h5>
                    <div class="row g-3 mb-4">
                        <div class="col-md-6">
                            <label class="form-label small fw-bold text-muted">Username (Permanent)</label>
                            <input type="text" class="form-control bg-light border-0" value="{{ user.username }}" readonly>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-bold text-muted">Email Address (Permanent)</label>
                            <input type="text" class="form-control bg-light border-0" value="{{ user.email }}" readonly>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-bold">First Name</label>
                            {{ u_form.first_name }}
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-bold">Last Name</label>
                            {{ u_form.last_name }}
                        </div>
                    </div>

                    <h5 class="fw-bold mb-3 border-bottom pb-2">Profile Details</h5>
                    <div class="row g-3 mb-4">
                        <div class="col-12">
                            <label class="form-label small fw-bold">Bio</label>
                            {{ p_form.bio }}
                            <div class="form-text">Write a short bio about yourself.</div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-bold">Blood Group</label>
                            {{ p_form.blood_group }}
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-bold">Phone</label>
                            {{ p_form.phone }}
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-bold">Birth Date</label>
                            {{ p_form.birth_date }}
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-bold">Location</label>
                            {{ p_form.location }}
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-bold">Profile Picture</label>
                            {{ p_form.profile_pic }}
                        </div>
                    </div>

                    <div class="d-grid mb-5">
                        <button type="submit" class="btn btn-danger py-2 fw-bold">
                            <i class="bi bi-check-circle me-2"></i>Update Profile
                        </button>
                    </div>
                </form>

                <h5 class="fw-bold mb-3 border-bottom pb-2">
                    <i class="bi bi-clock-history me-2 text-danger"></i>Transaction & Operation History
                </h5>
                
                <div class="mb-5">
                    <h6 class="fw-bold text-muted small mb-3">Donor Record (People You've Helped)</h6>
                    <div class="stats-mini bg-light p-3 rounded mb-3">
                        <div class="row text-center">
                            <div class="col-6 border-end">
                                <div class="h4 mb-0 fw-bold text-danger">{{ donations_made.count }}</div>
                                <div class="small text-muted">Total Volunteers</div>
                            </div>
                            <div class="col-6">
                                <div class="h4 mb-0 fw-bold text-success">{{ completed_donations_count }}</div>
                                <div class="small text-muted">Successful Donations</div>
                            </div>
                        </div>
                    </div>
                    
                    {% if donations_made %}
                        <div class="list-group list-group-flush border rounded overflow-hidden">
                            {% for donation in donations_made %}
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <span class="fw-bold">Helped {{ donation.request.patient_name }}</span>
                                            <div class="small text-muted">{{ donation.request.hospital }} | {{ donation.date|date:"M d, Y" }}</div>
                                        </div>
                                        <div>
                                            {% if donation.is_completed %}
                                                <span class="badge bg-success">Completed</span>
                                            {% else %}
                                                <span class="badge bg-warning text-dark">Pending</span>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <div class="text-center p-4 bg-light rounded border border-dashed">
                            <p class="text-muted small mb-0">You haven't volunteered for any requests yet.</p>
                        </div>
                    {% endif %}
                </div>

                <div class="mb-5">
                    <h6 class="fw-bold text-muted small mb-3">Request History (Who Helped You)</h6>
                    {% if requests_made %}
                        <div class="list-group list-group-flush border rounded overflow-hidden">
                            {% for br in requests_made %}
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div>
                                            <span class="fw-bold">{{ br.blood_group }} for {{ br.patient_name }}</span>
                                            <div class="small text-muted">{{ br.created_at|date:"M d, Y" }} | Status: <strong>{{ br.status }}</strong></div>
                                            
                                            <div class="mt-2">
                                                <p class="small fw-bold mb-1">Volunteers:</p>
                                                {% with br.donations.all as donations %}
                                                    {% if donations %}
                                                        <div class="d-flex flex-wrap gap-2">
                                                            {% for d in donations %}
                                                                <a href="{% url 'public_profile' d.donor_user.username %}" class="btn btn-sm btn-outline-danger py-0 px-2 rounded-pill small">
                                                                    <i class="bi bi-person-heart me-1"></i>{{ d.donor_user.username }}
                                                                </a>
                                                            {% endfor %}
                                                        </div>
                                                    {% else %}
                                                        <span class="text-muted small italic">No volunteers yet</span>
                                                    {% endif %}
                                                {% endwith %}
                                            </div>
                                        </div>
                                        <span class="badge {% if br.status == 'Active' %}bg-danger{% elif br.status == 'Accepted' %}bg-info{% else %}bg-secondary{% endif %}">
                                            {{ br.status }}
                                        </span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <div class="text-center p-4 bg-light rounded border border-dashed">
                            <p class="text-muted small mb-0">You haven't made any blood requests yet.</p>
                        </div>
                    {% endif %}
                </div>

                <div class="mt-5 border-top pt-4">
                    <h5 class="text-danger fw-bold mb-3">
                        <i class="bi bi-exclamation-triangle-fill me-2"></i>Danger Zone
                    </h5>
                    <div class="p-3 border border-danger border-opacity-25 rounded bg-danger bg-opacity-10">
                        <p class="small mb-3">Clearing personal info will remove your bio, location, phone number, and profile picture. Your account and donation history will remain intact.</p>
                        <form action="{% url 'delete_personal_info' %}" method="POST" onsubmit="return confirm('Are you sure you want to clear your personal information? This cannot be undone.');" class="mb-2">
                            {% csrf_token %}
                            <button type="submit" class="btn btn-outline-danger btn-sm w-100 mb-2">
                                Clear Personal Information
                            </button>
                        </form>
                        <form action="{% url 'delete_account' %}" method="POST" onsubmit="return confirm('WARNING: Are you sure you want to delete your account? This will permanently remove all your data, including messages and donation history. This action cannot be undone.');">
                            {% csrf_token %}
                            <button type="submit" class="btn btn-danger btn-sm w-100">
                                <i class="bi bi-trash-fill me-1"></i>Delete My Account Permanently
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
