File size: 6,434 Bytes
5774aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Uplan UI β€” Intake Flow (Step 0)

Three-path document intake:
  Path A: Auto-generated checklist from visa context (BYOG RAG in production)
  Path B: User pastes their own checklist
  Path C: Free upload with document type label required

Returns (VisaContext, list[UploadedDoc]) when ready, None if still collecting.
"""

from __future__ import annotations

from typing import Optional

import streamlit as st

from rules.context_profiles import (
    APPLICANT_COUNTRIES,
    DESTINATIONS,
    VISA_TYPES,
    VisaContext,
    get_context,
)


def _render_context_collection() -> Optional[VisaContext]:
    """Step 0: Collect applicant country, destination, visa type."""
    st.markdown("### 🌍 Application Context")
    st.caption("Tell us about the visa application β€” this determines which rules and thresholds apply.")

    col1, col2, col3 = st.columns(3)
    with col1:
        applicant_country = st.selectbox(
            "Applicant Country",
            options=APPLICANT_COUNTRIES,
            index=0,
            help="Country of citizenship/residence of the applicant",
        )
    with col2:
        destination = st.selectbox(
            "Destination Country",
            options=DESTINATIONS,
            index=0,
            help="Country the applicant is applying to",
        )
    with col3:
        visa_type = st.selectbox(
            "Visa Type",
            options=VISA_TYPES,
            index=0,
            help="Type of visa being applied for",
        )

    context = get_context(visa_type, destination)
    context.applicant_country = applicant_country

    # Show resolved thresholds
    with st.expander("πŸ“‹ Resolved Thresholds for This Context", expanded=False):
        st.json(context.to_dict())

    return context


def _render_checklist_upload(context: VisaContext) -> Optional[list[dict]]:
    """
    Three-path upload flow.
    Returns list of {"label": str, "pdf_bytes": bytes} when files are ready.
    """
    st.markdown("### πŸ“ Document Upload")

    path = st.radio(
        "Choose your upload method:",
        options=[
            "πŸ“‹ Use auto-generated checklist",
            "πŸ“ Paste my own checklist",
            "πŸ“€ Free upload (I know what I'm uploading)",
        ],
        index=0,
        horizontal=True,
    )

    uploads: list[dict] = []

    if path.startswith("πŸ“‹"):
        # ── Path A: Auto-generated checklist from context profile ───
        required = context.required_doc_types
        if not required:
            st.warning("No required documents defined for this visa profile.")
            required = ["passport", "bank_statement"]

        st.markdown(
            f"**Required documents for {context.visa_type.title()} β†’ "
            f"{context.destination.title()}:**"
        )

        for doc_type in required:
            label = doc_type.replace("_", " ").title()
            file = st.file_uploader(
                f"πŸ“„ {label}",
                type=["pdf"],
                key=f"checklist_{doc_type}",
                help=f"Upload your {label}",
            )
            if file:
                uploads.append({
                    "label": doc_type,
                    "pdf_bytes": file.read(),
                    "filename": file.name,
                })

        # Show progress
        uploaded_count = len(uploads)
        total_required = len(required)
        st.progress(
            uploaded_count / max(total_required, 1),
            text=f"{uploaded_count}/{total_required} required documents uploaded",
        )

    elif path.startswith("πŸ“"):
        # ── Path B: User pastes their own checklist ─────────────────
        checklist_text = st.text_area(
            "Paste your document checklist (one item per line):",
            placeholder="Bank Statement\nPassport\nEmployment Letter\nSponsor Letter",
            height=150,
        )

        if checklist_text.strip():
            items = [line.strip() for line in checklist_text.strip().split("\n") if line.strip()]
            for item in items:
                # Normalize to snake_case for internal use
                normalized = item.lower().replace(" ", "_")
                file = st.file_uploader(
                    f"πŸ“„ {item}",
                    type=["pdf"],
                    key=f"custom_{normalized}",
                )
                if file:
                    uploads.append({
                        "label": normalized,
                        "pdf_bytes": file.read(),
                        "filename": file.name,
                    })

    else:
        # ── Path C: Free upload with label dropdown ─────────────────
        st.caption("Upload files and label each one with its document type.")
        free_files = st.file_uploader(
            "Upload documents",
            type=["pdf"],
            accept_multiple_files=True,
            key="free_upload",
        )

        if free_files:
            doc_type_options = [
                "bank_statement", "payslip", "tax_return", "passport",
                "sponsor_letter", "employment_letter", "enrollment_letter",
                "affidavit", "other",
            ]
            for i, file in enumerate(free_files):
                col1, col2 = st.columns([3, 1])
                with col1:
                    st.text(f"πŸ“„ {file.name}")
                with col2:
                    label = st.selectbox(
                        "Type",
                        options=doc_type_options,
                        key=f"label_{i}",
                        label_visibility="collapsed",
                    )
                uploads.append({
                    "label": label,
                    "pdf_bytes": file.read(),
                    "filename": file.name,
                })

    return uploads if uploads else None


def run_intake() -> Optional[tuple[VisaContext, list[dict]]]:
    """
    Complete intake flow: context collection + document upload.

    Returns:
        (VisaContext, list[{"label": str, "pdf_bytes": bytes, "filename": str}])
        when the user has provided context and uploaded files.
        None if still collecting.
    """
    context = _render_context_collection()
    st.markdown("---")
    uploads = _render_checklist_upload(context)

    if uploads:
        return context, uploads
    return None