# Deployment — Hostinger (step by step)

You already have a Hostinger account (`u421442759@de-fra-web1809.main-hosting.eu`).
Below is the fastest clean path. Follow in order.

---

## 1 — Create the database

1. hPanel → **Databases → MySQL Databases**
2. **Create new database**
   - Name: `maycash_franchise` (or similar — the prefix is auto-added, e.g. `u421..._franchise`)
   - User: create a new one (note the username + password)
   - Grant **all privileges** on the new DB
3. Note the full values:
   - Host: usually `localhost`
   - DB name: e.g. `u421442759_franchise`
   - User: e.g. `u421442759_app`
   - Password: strong random one

## 2 — Import the schema

1. hPanel → **Databases → phpMyAdmin → Enter**
2. Pick your DB on the left
3. **Import tab** → upload `database/schema.sql` → **Go**
4. You should see tables: `submissions`, `admins`, `login_attempts`
5. Confirm an `admin` row exists in `admins`.

## 3 — Upload files

Option A — **File Manager** (drag & drop, easiest)
1. hPanel → **Files → File Manager**
2. Navigate to `public_html/` (or the subdomain folder)
3. Upload the **contents of `public/`** (the index.php, assets/, etc.) into `public_html/`
4. Create a folder **above** `public_html/` — e.g. `/home/u421442759/private/` — and upload the `src/` and `database/` folders there. **Do NOT put them inside public_html.**
5. Edit `public_html/index.php`, `login.php`, `dashboard.php`, `export.php`, `logout.php` top `require_once` paths so they point to `/home/u421442759/private/src/...`. OR simpler:

**Simpler layout (recommended for Hostinger)** — keep the app folder as-is and point the domain's document root to `public/`:

1. Upload the ENTIRE `franchise-app/` folder into `/home/u421442759/` (outside `public_html`).
2. hPanel → **Domains** → your domain → **Change document root** → set to `/home/u421442759/franchise-app/public`.
3. Restart. Now everything works untouched — `src/` stays private above the web root.

Option B — **SFTP** (faster if you have lots of files)
```
Host:  your-server.hosting.com
Port:  22 (some Hostinger uses 65002 — check hPanel)
User:  u421442759
Pass:  your hPanel password
```
Drop the folder in one go.

## 4 — Configure DB credentials

Edit `franchise-app/src/config.php` and fill in the values from step 1:

```php
'db' => [
    'host' => 'localhost',
    'name' => 'u421442759_franchise',
    'user' => 'u421442759_app',
    'pass' => 'the-password-you-set',
    'charset' => 'utf8mb4',
],
```

## 5 — First login + password reset

1. Browse to **https://yourdomain.com/login.php**
2. Login with **admin** / **ChangeMe2026!**
3. Immediately change the password:

### Change the admin password (SSH)
```bash
# SSH into Hostinger (port 65002)
ssh -p 65002 u421442759@your-server

cd franchise-app
php tools/hash-password.php "YourNewStrongPassword!"
# → copy the output hash

# Open phpMyAdmin in hPanel and run:
UPDATE admins SET password_hash = '<paste-hash-here>' WHERE username = 'admin';
```

### Or without SSH (no CLI access)
- hPanel → phpMyAdmin → run this SQL (password = `YourNewStrongPassword!`):

```sql
-- Generate the hash externally first using any PHP 8 bcrypt tool,
-- e.g. https://bcrypt-generator.com/ (cost 12), then:
UPDATE admins SET password_hash = '$2y$12$...' WHERE username = 'admin';
```

## 6 — Enable HTTPS

- hPanel → **SSL → Install** (Let's Encrypt, free, automatic)
- After SSL is active, open `public/.htaccess` and uncomment the HTTPS redirect block.

## 7 — Test the form

- Open the public URL
- Submit a test candidature
- Log in to `/dashboard.php` — the submission should appear
- Click **Exporter (CSV)** — should download a clean CSV

---

## File permissions checklist

If you get 500 errors, check permissions via File Manager:
- Folders: `755`
- PHP files: `644`
- `src/config.php`: **600** (owner read/write only) — protects DB credentials

## Common issues

| Problem | Fix |
|---|---|
| **Blank white page / 500** | Check `error_log` in File Manager; usually a DB connection error → re-verify `src/config.php` |
| **"Access denied for user"** | Credentials wrong, or the user doesn't have privileges on the DB |
| **Login keeps refreshing** | Your host is stripping cookies. Open `src/helpers.php` and set `'secure' => false` temporarily if you don't have HTTPS yet. Turn it back on after SSL. |
| **"Session invalide"** | Clock skew or first visit — refresh the page once |
| **Can't reach `/src/`** | Good — that's by design, it must not be web-accessible |

---

## Auto-deploy from GitHub

Your Hostinger Git panel already deploys from `yatlagh/yunik`. To deploy THIS app:

1. Push this `franchise-app/` folder to a GitHub repo (public or with PAT for private).
2. In hPanel → **Git Version Control**, add the repo:
   - URL: `https://github.com/yatlagh/maycash-franchise.git`
   - Branch: `main`
   - Install path: `/franchise-app` (or wherever you want it deployed)
3. Enable **Auto Deployment** — every push to `main` updates the site.
4. Make sure `src/config.php` on the server is **NOT** overwritten by Git. Options:
   - Keep `config.php` out of the repo (add to `.gitignore`) and edit it manually on the server.
   - Or use environment variables via hPanel → PHP config → Environment Variables (`DB_HOST`, `DB_NAME`, `DB_USER`, `DB_PASS`).
