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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python3
"""
Vietnamese Legal Corpus Downloader
Downloads laws and codes from thuvienphapluat.vn
"""

import re
import time
import sqlite3
from pathlib import Path
from datetime import datetime
from urllib.parse import urljoin

import httpx
import click
from bs4 import BeautifulSoup
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TaskProgressColumn

console = Console()

BASE_URL = "https://thuvienphapluat.vn"
SEARCH_URL = "https://thuvienphapluat.vn/page/tim-van-ban.aspx"
DATA_DIR = Path(__file__).parent.parent / "data"
DB_PATH = Path(__file__).parent.parent / "vietnam_laws.sqlite"

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "vi-VN,vi;q=0.9,en;q=0.8",
    "Referer": "https://thuvienphapluat.vn/",
}


def slugify(text: str) -> str:
    """Convert Vietnamese text to slug."""
    # Vietnamese character mapping
    vietnamese_map = {
        'à': 'a', 'á': 'a', 'ả': 'a', 'ã': 'a', 'ạ': 'a',
        'ă': 'a', 'ằ': 'a', 'ắ': 'a', 'ẳ': 'a', 'ẵ': 'a', 'ặ': 'a',
        'â': 'a', 'ầ': 'a', 'ấ': 'a', 'ẩ': 'a', 'ẫ': 'a', 'ậ': 'a',
        'đ': 'd',
        'è': 'e', 'é': 'e', 'ẻ': 'e', 'ẽ': 'e', 'ẹ': 'e',
        'ê': 'e', 'ề': 'e', 'ế': 'e', 'ể': 'e', 'ễ': 'e', 'ệ': 'e',
        'ì': 'i', 'í': 'i', 'ỉ': 'i', 'ĩ': 'i', 'ị': 'i',
        'ò': 'o', 'ó': 'o', 'ỏ': 'o', 'õ': 'o', 'ọ': 'o',
        'ô': 'o', 'ồ': 'o', 'ố': 'o', 'ổ': 'o', 'ỗ': 'o', 'ộ': 'o',
        'ơ': 'o', 'ờ': 'o', 'ớ': 'o', 'ở': 'o', 'ỡ': 'o', 'ợ': 'o',
        'ù': 'u', 'ú': 'u', 'ủ': 'u', 'ũ': 'u', 'ụ': 'u',
        'ư': 'u', 'ừ': 'u', 'ứ': 'u', 'ử': 'u', 'ữ': 'u', 'ự': 'u',
        'ỳ': 'y', 'ý': 'y', 'ỷ': 'y', 'ỹ': 'y', 'ỵ': 'y',
    }

    text = text.lower()
    for viet, ascii_char in vietnamese_map.items():
        text = text.replace(viet, ascii_char)

    # Replace spaces and special chars with hyphens
    text = re.sub(r'[^a-z0-9]+', '-', text)
    text = re.sub(r'-+', '-', text)
    return text.strip('-')


def create_front_matter(metadata: dict) -> str:
    """Create YAML front matter for markdown file."""
    lines = ["---"]
    for key, value in metadata.items():
        if value is not None:
            if isinstance(value, str) and ('\n' in value or ':' in value or '"' in value):
                # Multi-line or special chars: use quoted string
                value = value.replace('"', '\\"')
                lines.append(f'{key}: "{value}"')
            else:
                lines.append(f"{key}: {value}")
    lines.append("---")
    return "\n".join(lines)


def search_law_url(client: httpx.Client, name_vi: str, doc_number: str | None = None) -> str | None:
    """Search for a law on thuvienphapluat.vn and return its URL."""
    try:
        # Search by document number first if available
        keyword = doc_number if doc_number else name_vi
        params = {
            "keyword": keyword,
            "type": "0",
            "match": "True",
            "area": "0",
            "status": "0",
            "signer": "0",
            "sort": "0",
            "page": "1",
        }

        response = client.get(SEARCH_URL, params=params, follow_redirects=True)
        response.raise_for_status()

        soup = BeautifulSoup(response.text, "lxml")

        # Find first matching result
        for item in soup.select(".nqDoc, .doc-item, .search-result-item, [class*='item']"):
            link = item.select_one("a[href*='/van-ban/']")
            if link:
                href = link.get("href", "")
                if href:
                    return urljoin(BASE_URL, href)

        # Try alternative selectors
        for link in soup.select("a[href*='/van-ban/']"):
            href = link.get("href", "")
            if href and "/van-ban/" in href:
                return urljoin(BASE_URL, href)

        return None

    except Exception as e:
        console.print(f"[yellow]Search failed for {name_vi}: {e}[/yellow]")
        return None


def fetch_law_content(client: httpx.Client, url: str) -> dict | None:
    """Fetch law content from thuvienphapluat.vn."""
    try:
        response = client.get(url, follow_redirects=True)
        response.raise_for_status()

        soup = BeautifulSoup(response.text, "lxml")

        # Extract metadata
        metadata = {
            "url": url,
            "downloaded_at": datetime.now().isoformat(),
        }

        # Title
        title_elem = soup.select_one("h1.title, .doc-title h1, h1")
        if title_elem:
            metadata["title"] = title_elem.get_text(strip=True)

        # Document number
        doc_num = soup.select_one(".doc-number, .so-hieu")
        if doc_num:
            metadata["document_number"] = doc_num.get_text(strip=True)

        # Effective date
        effective = soup.select_one(".effective-date, .ngay-hieu-luc")
        if effective:
            metadata["effective_date"] = effective.get_text(strip=True)

        # Status
        status = soup.select_one(".doc-status, .tinh-trang")
        if status:
            metadata["status"] = status.get_text(strip=True)

        # Issuing body
        issuer = soup.select_one(".issuing-body, .co-quan-ban-hanh")
        if issuer:
            metadata["issuing_body"] = issuer.get_text(strip=True)

        # Main content
        content_elem = soup.select_one(".doc-content, .noi-dung, .content, article")
        if content_elem:
            # Remove scripts and styles
            for tag in content_elem.select("script, style"):
                tag.decompose()

            content = content_elem.get_text(separator="\n", strip=True)
            metadata["content"] = content
        else:
            # Fallback: get body text
            body = soup.find("body")
            if body:
                for tag in body.select("script, style, nav, header, footer"):
                    tag.decompose()
                metadata["content"] = body.get_text(separator="\n", strip=True)

        return metadata

    except Exception as e:
        console.print(f"[red]Error fetching {url}: {e}[/red]")
        return None


def fetch_law_list_page(client: httpx.Client, page: int = 1) -> list[dict]:
    """Fetch list of laws from thuvienphapluat.vn."""
    url = f"{BASE_URL}/page/tim-van-ban.aspx?keyword=&type=0&match=True&area=0&status=0&signer=0&sort=0&page={page}"

    try:
        response = client.get(url, follow_redirects=True)
        response.raise_for_status()

        soup = BeautifulSoup(response.text, "lxml")
        laws = []

        for item in soup.select(".doc-item, .search-result-item, .item"):
            law = {}

            link = item.select_one("a[href*='/van-ban/']")
            if link:
                law["url"] = urljoin(BASE_URL, link.get("href", ""))
                law["title"] = link.get_text(strip=True)

            doc_num = item.select_one(".doc-number, .so-hieu")
            if doc_num:
                law["document_number"] = doc_num.get_text(strip=True)

            if law.get("url"):
                laws.append(law)

        return laws

    except Exception as e:
        console.print(f"[red]Error fetching law list page {page}: {e}[/red]")
        return []


def get_laws_from_db() -> list[dict]:
    """Get laws from SQLite database."""
    if not DB_PATH.exists():
        console.print(f"[yellow]Database not found: {DB_PATH}[/yellow]")
        return []

    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()

    laws = []

    # Get codes
    cursor.execute("SELECT * FROM codes")
    for row in cursor.fetchall():
        laws.append({
            "id": f"code-{row['id']}",
            "type": "code",
            "name": row["name"],
            "name_vi": row["name_vi"],
            "year": row["year"],
            "document_number": row["document_number"],
            "effective_date": row["effective_date"],
            "status": row["status"],
            "url": row["url"],
        })

    # Get laws
    cursor.execute("SELECT * FROM laws")
    for row in cursor.fetchall():
        laws.append({
            "id": f"law-{row['id']}",
            "type": "law",
            "name": row["name"],
            "name_vi": row["name_vi"],
            "year": row["year"],
            "document_number": row["document_number"],
            "effective_date": row["effective_date"],
            "status": row["status"],
            "url": row["url"],
        })

    conn.close()
    return laws


def save_law_file(law: dict, content: str | None = None) -> Path:
    """Save law to markdown file with front matter."""
    DATA_DIR.mkdir(parents=True, exist_ok=True)

    # Generate filename with year prefix for uniqueness
    law_type = law.get("type", "law")
    year = law.get("year", "unknown")
    name_vi = law.get("name_vi", law.get("name", "unknown"))
    slug = slugify(name_vi)

    # Prefix: code- or law- with year
    prefix = "code" if law_type == "code" else "law"
    filename = f"{prefix}-{year}-{slug}.md"
    filepath = DATA_DIR / filename

    # Prepare front matter
    front_matter_data = {
        "title": law.get("name_vi", law.get("name")),
        "title_en": law.get("name"),
        "type": law_type,
        "year": year,
        "document_number": law.get("document_number"),
        "effective_date": law.get("effective_date"),
        "status": law.get("status", "Active"),
        "url": law.get("url"),
        "downloaded_at": datetime.now().isoformat(),
    }

    # Build markdown content
    md_content = create_front_matter(front_matter_data)
    md_content += "\n\n"
    md_content += f"# {law.get('name_vi', law.get('name'))}\n\n"

    if law.get("name"):
        md_content += f"**English:** {law['name']}\n\n"

    if law.get("document_number"):
        md_content += f"**Số hiệu:** {law['document_number']}\n\n"

    if law.get("effective_date"):
        md_content += f"**Ngày hiệu lực:** {law['effective_date']}\n\n"

    if content:
        md_content += "## Nội dung\n\n"
        md_content += content
    else:
        md_content += "*Nội dung chưa được tải xuống.*\n"

    filepath.write_text(md_content, encoding="utf-8")
    return filepath


@click.group()
def cli():
    """Vietnamese Legal Corpus Downloader"""
    pass


@cli.command()
@click.option("--fetch-content", is_flag=True, help="Fetch full content from web")
@click.option("--delay", default=1.0, help="Delay between requests (seconds)")
@click.option("--limit", default=0, help="Limit number of laws to download (0 = all)")
def download(fetch_content: bool, delay: float, limit: int):
    """Download laws from database to markdown files."""
    console.print("[bold blue]Vietnamese Legal Corpus Downloader[/bold blue]")
    console.print()

    # Get laws from database
    laws = get_laws_from_db()

    if not laws:
        console.print("[yellow]No laws found in database. Please populate the database first.[/yellow]")
        return

    if limit > 0:
        laws = laws[:limit]

    console.print(f"Found [green]{len(laws)}[/green] laws/codes to download")
    console.print(f"Output directory: [cyan]{DATA_DIR}[/cyan]")
    console.print()

    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        BarColumn(),
        TaskProgressColumn(),
        console=console,
    ) as progress:
        task = progress.add_task("Downloading...", total=len(laws))

        client = httpx.Client(headers=HEADERS, timeout=30.0)

        try:
            for law in laws:
                name = law.get("name_vi", law.get("name", "Unknown"))
                progress.update(task, description=f"[cyan]{name[:40]}[/cyan]")

                content = None
                url = law.get("url")

                if fetch_content:
                    # Search for URL if not in database
                    if not url:
                        url = search_law_url(
                            client,
                            law.get("name_vi", ""),
                            law.get("document_number")
                        )
                        time.sleep(delay)

                    # Fetch content from URL
                    if url:
                        law["url"] = url
                        data = fetch_law_content(client, url)
                        if data:
                            content = data.get("content")
                        time.sleep(delay)

                filepath = save_law_file(law, content)
                progress.advance(task)

        finally:
            client.close()

    console.print()
    console.print(f"[green]Done![/green] Files saved to [cyan]{DATA_DIR}[/cyan]")


@cli.command()
def stats():
    """Show statistics from database."""
    laws = get_laws_from_db()

    if not laws:
        console.print("[yellow]No data in database[/yellow]")
        return

    codes = [l for l in laws if l["type"] == "code"]
    law_list = [l for l in laws if l["type"] == "law"]

    console.print("[bold]Vietnamese Legal Corpus Statistics[/bold]")
    console.print()
    console.print(f"  Codes (Bộ luật): [green]{len(codes)}[/green]")
    console.print(f"  Laws (Luật):     [green]{len(law_list)}[/green]")
    console.print(f"  [bold]Total:           [green]{len(laws)}[/green][/bold]")

    # By year
    console.print()
    console.print("[bold]By Year:[/bold]")
    years = {}
    for law in laws:
        year = law.get("year", "Unknown")
        years[year] = years.get(year, 0) + 1

    for year in sorted(years.keys(), reverse=True)[:10]:
        console.print(f"  {year}: {years[year]}")


@cli.command()
def list_files():
    """List downloaded files."""
    if not DATA_DIR.exists():
        console.print("[yellow]No data directory found[/yellow]")
        return

    files = list(DATA_DIR.rglob("*.md"))
    console.print(f"Found [green]{len(files)}[/green] files in {DATA_DIR}")

    for f in files[:20]:
        console.print(f"  {f.relative_to(DATA_DIR)}")

    if len(files) > 20:
        console.print(f"  ... and {len(files) - 20} more")


def main():
    cli()


if __name__ == "__main__":
    main()