$file = "d:\workspace_vs1\owrainfo_www\owrawww\src\main\resources\templates\sub04_03.html"
$enc  = [System.Text.Encoding]::UTF8
$c    = [System.IO.File]::ReadAllText($file, $enc)

# ---- 1. Head: add board.css ----
$h_old = "<head>`r`n    <title>보도자료</title>`r`n<body>"
$h_new = "<head>`r`n    <title>보도자료</title>`r`n    <th:block layout:fragment=`"head-extra`">`r`n        <link rel=`"stylesheet`" href=`"/css/board.css`">`r`n    </th:block>`r`n</head>`r`n<body>"
$c = $c.Replace($h_old, $h_new)
Write-Host "1. Head: $([bool]($c -match 'board.css'))"

# ---- 2. Depth1 breadcrumb label + links (8 tabs before button/ul, 9 tabs before inner) ----
$tab8 = "`t" * 8
$tab9 = "`t" * 9
$tab10 = "`t" * 10
$crlf = "`r`n"

$d1old = "$tab8<button class=`"bc-btn`" type=`"button`" aria-haspopup=`"listbox`" aria-expanded=`"false`">$crlf" +
         "$tab9<span class=`"bc-label`">사업소개</span>$crlf" +
         "$tab9<span class=`"bc-arrow`">$crlf" +
         "$tab10<img src=`"/img/sub/ico_arrow.svg`" alt=`"arrow`">$crlf" +
         "$tab9</span>$crlf" +
         "$tab8</button>$crlf" +
         "$tab8<ul class=`"bc-dropdown`" role=`"listbox`">$crlf" +
         "$tab9<li><a href=`"/sub/sub1_01`">회사소개</a></li>$crlf" +
         "$tab9<li><a href=`"/sub/sub1_01`">사업소개</a></li>$crlf" +
         "$tab9<li><a href=`"/sub/sub3_01`">인재채용</a></li>$crlf" +
         "$tab9<li class=`"active`"><a href=`"/sub/sub4_01`">오라인</a></li>$crlf" +
         "$tab8</ul>"

$d1new = "$tab8<button class=`"bc-btn`" type=`"button`" aria-haspopup=`"listbox`" aria-expanded=`"false`">$crlf" +
         "$tab9<span class=`"bc-label`">오라인</span>$crlf" +
         "$tab9<span class=`"bc-arrow`">$crlf" +
         "$tab10<img src=`"/img/sub/ico_arrow.svg`" alt=`"arrow`">$crlf" +
         "$tab9</span>$crlf" +
         "$tab8</button>$crlf" +
         "$tab8<ul class=`"bc-dropdown`" role=`"listbox`">$crlf" +
         "$tab9<li><a href=`"/company/intro`">회사소개</a></li>$crlf" +
         "$tab9<li><a href=`"/business/intro`">사업소개</a></li>$crlf" +
         "$tab9<li><a href=`"/careers/talent`">인재채용</a></li>$crlf" +
         "$tab9<li class=`"active`"><a href=`"/bbs/owrainfo`">오라인</a></li>$crlf" +
         "$tab8</ul>"

$before = $c.Length
$c = $c.Replace($d1old, $d1new)
Write-Host "2. Depth1: replaced=$($c.Length -ne $before)"

# ---- 3. Depth2 breadcrumb links ----
$d2old = "$tab8<ul class=`"bc-dropdown`" role=`"listbox`">$crlf" +
         "$tab9<li><a href=`"/sub4_01`">오라인 소식</a></li>$crlf" +
         "$tab9<li><a href=`"/sub4_02`">제휴문의</a></li>$crlf" +
         "$tab9<li class=`"active`"><a href=`"/sub4_03`">보도자료</a></li>$crlf" +
         "$tab8</ul>"

$d2new = "$tab8<ul class=`"bc-dropdown`" role=`"listbox`">$crlf" +
         "$tab9<li><a href=`"/bbs/owrainfo`">오라인 소식</a></li>$crlf" +
         "$tab9<li><a href=`"/bbs/partnership`">제휴문의</a></li>$crlf" +
         "$tab9<li class=`"active`"><a href=`"/bbs/press`">보도자료</a></li>$crlf" +
         "$tab8</ul>"

$before = $c.Length
$c = $c.Replace($d2old, $d2new)
Write-Host "3. Depth2: replaced=$($c.Length -ne $before)"

# ---- 4. Search form: div -> form with th: attributes ----
# Use regex to replace the whole board-search-wrap div through section 1 comment
$sfNew = @"
                        <form class="board-search-wrap" th:action="@{/bbs/press}" method="post">
                            <div class="board-search">
                                <div class="search-select-wrap">
                                    <select class="search-select" name="searchType" aria-label="검색 조건">
                                        <option value="title"   th:selected="`${searchType == 'title'}">제목</option>
                                        <option value="content" th:selected="`${searchType == 'content'}">내용</option>
                                        <option value="all"     th:selected="`${searchType == 'all'}">제목+내용</option>
                                    </select>
                                    <span class="select-arrow" aria-hidden="true">
                                        <span class="material-symbols-outlined">expand_more</span>
                                    </span>
                                </div>
                                <div class="search-input-wrap">
                                    <input type="text" name="keyword" class="search-input"
                                           placeholder="검색어를 입력하세요" aria-label="검색어 입력"
                                           th:value="`${keyword}">
                                </div>
                                <button type="submit" class="search-btn" aria-label="검색">
                                    <span class="material-symbols-outlined">search</span>
                                </button>
                            </div>
                        </form>
                        <!-- // SECTION 1 : 검색 영역 -->
"@

# Escape $ in replacement string for [regex]::Replace
$sfNewEsc = $sfNew.Replace('$', '$$')
$before = $c.Length
$c = [regex]::Replace($c, '(?s)<div class="board-search-wrap">[\s\S]*?<!-- // SECTION 1[^>]*>', $sfNewEsc, 1)
Write-Host "4. SearchForm: replaced=$($c.Length -ne $before)"

# ---- 5. Gallery list: static items -> th:each ----
$galleryNew = @"
                        <div class="board-list-wrap">
                            <ul class="board-gallery-wrap" aria-label="갤러리 목록">

                                <th:block th:if="`${#lists.isEmpty(list)}">
                                    <li class="gallery-empty">
                                        <span class="material-symbols-outlined">image_not_supported</span>
                                        <p>등록된 게시물이 없습니다.</p>
                                    </li>
                                </th:block>

                                <li th:each="item : `${list}">
                                    <a th:href="@{/bbs/press/view(id=`${item.id},searchType=`${searchType},keyword=`${keyword},page=`${currentPage})}"
                                       class="gallery-card">
                                        <div class="gallery-card__thumb">
                                            <img th:src="`${#strings.isEmpty(item.mainImg) ? '/img/common/no_img.jpg' : item.mainImg}"
                                                 th:alt="`${item.title}">
                                        </div>
                                        <div class="gallery-card__body">
                                            <p class="gallery-card__title" th:text="`${item.title}"></p>
                                        </div>
                                    </a>
                                </li>

                            </ul>
                        </div>
                        <!-- // SECTION 2 : 갤러리 리스트 -->
"@

$galleryNewEsc = $galleryNew.Replace('$', '$$')
$before = $c.Length
$c = [regex]::Replace($c, '(?s)<div class="board-list-wrap">[\s\S]*?<!-- // SECTION 2[^>]*>', $galleryNewEsc, 1)
Write-Host "5. Gallery: replaced=$($c.Length -ne $before)"

# ---- 6. Pagination: static buttons -> th:href links ----
$pgNew = @"
                        <div class="board-pagination-wrap">
                            <nav class="board-pagination" aria-label="페이지 탐색">
                                <a th:href="@{/bbs/press(page=1,searchType=`${searchType},keyword=`${keyword})}"
                                   class="pg-btn pg-first" aria-label="첫 페이지">
                                    <span class="material-symbols-outlined">keyboard_double_arrow_left</span>
                                </a>
                                <a th:href="@{/bbs/press(page=`${currentPage > 1 ? currentPage - 1 : 1},searchType=`${searchType},keyword=`${keyword})}"
                                   class="pg-btn pg-prev" aria-label="이전 페이지">
                                    <span class="material-symbols-outlined">chevron_left</span>
                                </a>
                                <ul class="pg-list">
                                    <li th:each="p : `${#numbers.sequence(blockStart, blockEnd)}">
                                        <a th:href="@{/bbs/press(page=`${p},searchType=`${searchType},keyword=`${keyword})}"
                                           th:text="`${p}"
                                           th:class="`${p == currentPage ? 'pg-num active' : 'pg-num'}"
                                           th:aria-current="`${p == currentPage ? 'page' : null}"
                                           th:aria-label="`${p + '페이지'}"></a>
                                    </li>
                                </ul>
                                <a th:href="@{/bbs/press(page=`${currentPage < totalPages ? currentPage + 1 : totalPages},searchType=`${searchType},keyword=`${keyword})}"
                                   class="pg-btn pg-next" aria-label="다음 페이지">
                                    <span class="material-symbols-outlined">chevron_right</span>
                                </a>
                                <a th:href="@{/bbs/press(page=`${totalPages},searchType=`${searchType},keyword=`${keyword})}"
                                   class="pg-btn pg-last" aria-label="마지막 페이지">
                                    <span class="material-symbols-outlined">keyboard_double_arrow_right</span>
                                </a>
                            </nav>
                        </div>
                        <!-- // SECTION 3 : 페이지네이션 영역 -->
"@

$pgNewEsc = $pgNew.Replace('$', '$$')
$before = $c.Length
$c = [regex]::Replace($c, '(?s)<div class="board-pagination-wrap">[\s\S]*?<!-- // SECTION 3[^>]*>', $pgNewEsc, 1)
Write-Host "6. Pagination: replaced=$($c.Length -ne $before)"

[System.IO.File]::WriteAllText($file, $c, $enc)
Write-Host "Done. File saved."
