modules/frontend/Write-Frontend-NextJs-Pages.ps1

param(
    [string]$ProjectName,
    [string]$OrmMode,
    [string]$OutputPath,
    [string]$FrontendMode,
    [string[]]$Entities,
    [string]$Language = 'tr'
)

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")
. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Get-Locale.ps1")
$L = Get-Locale -Language $Language

$base = Join-Path $OutputPath "frontend"
function Write-File($rel, $rawContent) {
    Write-CoreFile (Join-Path $base $rel) $rawContent
}
function Proj($t) { $t -replace '__PROJ__', $ProjectName }

# ─── PAGES ────────────────────────────────────────────────
Write-File "app\login\page.tsx" (Proj @"
import { LoginForm } from '@/components/auth/LoginForm';
import styles from './LoginPage.module.css';
export default function LoginPage() {
  return (
    <div className={styles.wrapper}>
      <div className={styles.card}>
        <div className={styles.header}>
          <h1 className={styles.title}>__PROJ__</h1>
          <p className={styles.subtitle}>$($L.uiLoginSubtitle)</p>
        </div>
        <LoginForm />
      </div>
    </div>
  );
}
"@
)

Write-File "app\login\LoginPage.module.css" @'
.wrapper { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 55%, #0f3460 100%); padding: 20px; }
.card { background: #fff; border-radius: 16px; padding: 44px 40px; width: 100%; max-width: 400px; box-shadow: 0 24px 64px rgba(0,0,0,0.28); }
.header { margin-bottom: 28px; }
.title { font-size: 1.7rem; font-weight: 700; color: #0f3460; margin-bottom: 4px; }
.subtitle { color: #6b7280; font-size: 0.88rem; }
'@


# Main Redirect
$firstEntity = ($Entities | Where-Object { $_ -ne "User" } | Select-Object -First 1).ToLower() + "s"
Write-File "app\page.tsx" @"
import { redirect } from 'next/navigation';
export default function HomePage() { redirect('/$firstEntity'); }
"@


foreach ($e in $Entities) {
    if ($e -eq "User") { continue }
    $lowE = $e.ToLower()
    $dirE = $lowE + "s"

    Write-File "app\$dirE\page.tsx" (Proj @"
'use client';
import { useState, useEffect, useCallback } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { useAuth } from '@/context/AuthContext';
import { $e`Form } from '@/components/$dirE/$e`Form';
import { $e`Table } from '@/components/$dirE/$e`Table';
import { Button } from '@/components/common/Button';
import { $lowE`Service } from '@/services/$lowE`Service';
import styles from './Page.module.css';
export default function $e`Page() {
  const { username, isAuthenticated, logout } = useAuth();
  const router = useRouter();
  const [items, setItems] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);
  useEffect(() => {
    if (isAuthenticated === false) router.push('/login');
  }, [isAuthenticated, router]);
  const loadData = useCallback(async () => {
    setLoading(true);
    try { setItems(await $lowE`Service.getAll()); }
    catch (err: any) { if (err.message === 'UNAUTHORIZED') { logout(); router.push('/login'); } }
    finally { setLoading(false); }
  }, [logout, router]);
  useEffect(() => { if (isAuthenticated) loadData(); }, [isAuthenticated, loadData]);
  if (isAuthenticated === null || isAuthenticated === false) return null;
  return (
    <div className={styles.layout}>
      <header className={styles.topbar}>
        <div className={styles.left}>
            <span className={styles.brand}>__PROJ__</span>
            <nav className={styles.nav}>
                $( ($Entities | Where-Object { $_ -ne "User" } | ForEach-Object { "<Link href='/$($_.ToLower())s' className={styles.navLink}>$_</Link>" }) -join " " )
            </nav>
        </div>
        <div className={styles.user}>
          <span className={styles.username}>{username}</span>
          <Button variant="danger" onClick={() => { logout(); router.push('/login'); }} style={{ padding: '7px 16px', fontSize: '0.83rem' }}>$($L.uiLogout)</Button>
        </div>
      </header>
      <main className={styles.main}>
        <section className={styles.card}><h2 className={styles.sectionTitle}>$($L.uiNewEntity -f $e)</h2><$e`Form onAdded={loadData} /></section>
        <section className={styles.card}><h2 className={styles.sectionTitle}>$($L.uiEntityList -f $e)</h2><$e`Table items={items} loading={loading} /></section>
      </main>
    </div>
  );
}
"@
)
    
    Write-File "app\$dirE\Page.module.css" @'
.layout{display:flex;flex-direction:column;min-height:100vh;background:#f0f2f5;}
.topbar{background:#0f3460;color:#fff;padding:0 32px;height:58px;display:flex;align-items:center;justify-content:space-between;box-shadow:0 2px 8px rgba(0,0,0,.2);position:sticky;top:0;z-index:10;}
.left{display:flex;align-items:center;gap:32px;} .brand{font-size:1.05rem;font-weight:700;letter-spacing:.4px;}
.nav{display:flex;gap:16px;} .navLink{color:rgba(255,255,255,.7);font-size:.88rem;transition:color .2s;} .navLink:hover{color:#fff;}
.user{display:flex;align-items:center;gap:14px;} .username{font-size:.88rem;opacity:.85;}
.main{flex:1;padding:28px 32px;max-width:960px;width:100%;margin:0 auto;display:flex;flex-direction:column;gap:20px;}
.card{background:#fff;border-radius:12px;padding:26px 28px;box-shadow:0 2px 12px rgba(0,0,0,.07);}
.sectionTitle{font-size:1.05rem;font-weight:700;color:#0f3460;margin-bottom:18px;}
'@

}