# Plan: Improve High Settings Screen — Advanced Actions & Details

## Context
The current High Settings screen covers basic cache, migration, git, environment, and debug controls. The goal is to add advanced system operations, real-time status panels, better UX (SweetAlert2 confirmations, copy-to-clipboard, FormValidation), and detailed server/git info — making it a complete developer operations panel inside the admin.

---

## Critical Files

| Role | Path |
|---|---|
| Controller | `app/Http/Controllers/Admin/HighSettingsController.php` |
| View | `resources/views/dashbord/admin/HighSettings/index.blade.php` |
| Routes | `routes/admin.php` |
| Spec doc | `ai/admin/high_settings_actions.md` |
| Keen JS pattern | `public/assets/js/custom/apps/ecommerce/settings/settings.js` |
| Footer scripts | `resources/views/dashbord/layouts/footer-scripts.blade.php` |

---

## New Controller Methods (add to `HighSettingsController.php`)

### 1. `gitStatus()` — GET
- Run: `git status --short`, `git branch --show-current`, `git log --oneline -5`
- Return JSON: `{ branch, staged[], modified[], untracked[], recent_commits[] }`

### 2. `gitLog()` — GET
- Run: `git log --pretty=format:"%h|%s|%an|%ar" -15`
- Parse each line into `{ hash, message, author, date }`
- Return JSON array of commits

### 3. `optimizeApp()` — POST
- Run via `Artisan::call()`: `config:cache`, `route:cache`, `view:cache`
- Return JSON with combined output

### 4. `storageLink()` — POST
- Run: `Artisan::call('storage:link')`
- Return JSON with output

### 5. `viewLogs()` — GET
- Read last 100 lines of `storage/logs/laravel.log` using `file()` + `array_slice()`
- Return JSON: `{ lines: string }`

### 6. `serverInfo()` — GET
- Collect: PHP version, Laravel version (`app()->version()`), APP_ENV, APP_DEBUG, server OS, disk free/total (`disk_free_space`, `disk_total_space`)
- Return JSON object

### 7. `gitStash()` — POST
- Run: `git stash`
- Return JSON

### 8. `gitStashPop()` — POST
- Run: `git stash pop`
- Return JSON

### 9. `gitDiff()` — GET
- Run: `git diff --stat`
- Return JSON with diff output

---

## New Routes (add inside `high-settings` group in `routes/admin.php`)

```php
// Info (GET)
Route::get('git-status',   [HighSettingsController::class, 'gitStatus'])->name('git-status');
Route::get('git-log',      [HighSettingsController::class, 'gitLog'])->name('git-log');
Route::get('git-diff',     [HighSettingsController::class, 'gitDiff'])->name('git-diff');
Route::get('server-info',  [HighSettingsController::class, 'serverInfo'])->name('server-info');
Route::get('view-logs',    [HighSettingsController::class, 'viewLogs'])->name('view-logs');

// Actions (POST)
Route::post('optimize',       [HighSettingsController::class, 'optimizeApp'])->name('optimize');
Route::post('storage-link',   [HighSettingsController::class, 'storageLink'])->name('storage-link');
Route::post('git-stash',      [HighSettingsController::class, 'gitStash'])->name('git-stash');
Route::post('git-stash-pop',  [HighSettingsController::class, 'gitStashPop'])->name('git-stash-pop');
```

---

## View Changes (`index.blade.php`)

### New Cards to Add

#### A. Server Info Card (col-xl-12, top of page, auto-loads)
- Displays: PHP version, Laravel version, Environment, Debug mode, OS, Disk usage (progress bar)
- Loaded on `init()` via `_serverInfo()` GET request
- Refresh button to reload

#### B. Git Status Card (col-xl-8, auto-loads)
- Shows: current branch badge, counts of staged/modified/untracked files
- Three collapsible lists: Staged, Modified, Untracked
- "Refresh" button + auto-loads on `init()`
- Color-coded: staged=green, modified=yellow, untracked=gray

#### C. Git Diff Card (col-xl-4)
- Button "Show Diff" to load `git diff --stat`
- Output rendered in scrollable `<pre>`

#### D. Git Log Card (col-xl-12)
- Button "Load History"
- Renders a clean table: Hash | Message | Author | Time
- Hash column is monospace badge

#### E. Optimize Application Card (col-xl-6)
- Buttons: "Optimize All" (runs config+route+view cache), "Clear Optimize" (existing clear-cache)
- Result container below

#### F. Storage Link Card (col-xl-6)
- Button "Create Storage Link"
- Description of what it does

#### G. Laravel Logs Card (col-xl-12)
- Button "Load Recent Logs"
- Output in scrollable `<pre>` (max 400px), monospace
- Copy button on the output block

#### H. Git Stash (add to existing Git Repository card)
- Two buttons side by side: "Stash Changes" (secondary) + "Pop Stash" (outline-secondary)
- Result in shared `result-git-repo` container

### UX Improvements

#### 1. SweetAlert2 Confirmations
Wrap these actions with `Swal.fire({ ... }).then(result => { if (result.isConfirmed) ... })` before posting:
- Clear Cache
- Run Migrations
- Git Push
- Git Stash / Stash Pop

Swal config to use:
```js
const _confirm = (title, text, confirmText) =>
    Swal.fire({
        title, text, icon: 'warning',
        showCancelButton: true,
        buttonsStyling: false,
        confirmButtonText: confirmText,
        cancelButtonText: 'Cancel',
        customClass: { confirmButton: 'btn btn-danger', cancelButton: 'btn btn-light' }
    });
```

#### 2. Copy-to-Clipboard on Output Blocks
When `_showResult()` renders a `<pre>`, append a small "Copy" button:
```js
const _copyBtn = text =>
    `<button class="btn btn-sm btn-light-primary mt-2" onclick="navigator.clipboard.writeText(\`${text}\`)">
        <i class="fa fa-copy me-1"></i>Copy
    </button>`;
```

#### 3. FormValidation on Git Config Form
Use `FormValidation.formValidation()` (already globally available) to validate `git_name` (not empty) and `git_email` (email format) before submission. Only call `_post()` after `status == 'Valid'`.

#### 4. Auto-Load on Page Init
In `KTHighSettings.init()`, automatically call:
- `_serverInfo()` — populate server info card
- `_gitStatusLoad()` — populate git status card

---

## JavaScript Structure Changes

### New private functions to add inside `KTHighSettings`:

```js
const _serverInfo = () => { /* GET server-info, render info grid */ };
const _gitStatusLoad = () => { /* GET git-status, render branch + file lists */ };
const _gitLog = () => { /* GET git-log, render commit table */ };
const _gitDiff = () => { /* GET git-diff, render pre output */ };
const _optimizeApp = () => { /* POST optimize */ };
const _storageLink = () => { /* POST storage-link */ };
const _viewLogs = () => { /* GET view-logs, render pre output */ };
const _gitStash = () => { /* Swal confirm then POST git-stash */ };
const _gitStashPop = () => { /* Swal confirm then POST git-stash-pop */ };
const _confirm = (title, text, btn) => Swal.fire({ ... });
```

### Updated `init()`:
```js
init: () => {
    _initEventListeners();
    _initFormValidation();  // new
    _serverInfo();          // auto-load
    _gitStatusLoad();       // auto-load
},
```

### New public methods to expose:
`optimizeApp`, `storageLink`, `viewLogs`, `gitLog`, `gitDiff`, `gitStash`, `gitStashPop`, `refreshGitStatus`, `refreshServerInfo`

---

## Updated `ai/admin/high_settings_actions.md`
After implementation, rewrite this file to document all new actions, routes, controller methods, JS functions, and UI sections — matching the existing doc format.

---

## Verification

1. Visit `/admin/high-settings` — server info card and git status card should auto-populate on load
2. Click "Clear All Cache" — SweetAlert confirmation should appear before action runs
3. Click "Run Migrations" — SweetAlert confirmation should appear
4. Click "Push to GitHub" — SweetAlert confirmation should appear
5. Click "Load History" — git log table should render with 15 commits
6. Click "Show Diff" — git diff stat should render
7. Click "Load Recent Logs" — last 100 lines of laravel.log should appear
8. Click "Generate Thumbnails" — still works as before
9. Git config form — FormValidation should reject invalid email before submit
10. All output blocks — "Copy" button should copy text to clipboard
