# Deployment guide (Stage 1)

## 1. Requirements
- PHP 8.1+ with PDO MySQL, Fileinfo, JSON and OpenSSL enabled
- MySQL 8.0+ or compatible MariaDB
- HTTPS certificate
- Android Studio with a current stable Android SDK
- PHP hosting that permits a private writable upload directory

## 2. Database
1. Create a database and database user in your hosting control panel.
2. Import `database/schema.sql` using phpMyAdmin.
3. If your hosting provider does not permit `CREATE DATABASE`, remove the first two SQL lines and select the database in phpMyAdmin before importing.

## 3. Backend
1. Copy `backend/` to your server.
2. Copy `backend/config.example.php` to `backend/config.php`.
3. Set database host/name/user/password and the private upload directory.
4. Put the web document root at `backend/public` if possible. Keep `backend/admin` protected behind HTTPS and a strong admin password. A recommended deployment maps `/api` to `public/api` and `/admin` to the `admin` directory.
5. Ensure the private upload directory is outside public web root and writable by PHP (permissions typically 700 or a dedicated writable owner/group).
6. Run `php backend/bin/create_admin.php your_admin_username` from SSH/CLI. Do not create an admin password in source code.
7. Test `https://your-domain/api/health`.

## 4. Android
1. Open the `android/` folder in Android Studio.
2. Let Gradle sync and install on a physical Android device.
3. Change `BASE_URL` in `android/app/build.gradle.kts` to your HTTPS API base URL ending in `/api/`.
4. For a debug build use Build > Build Bundle(s) / APK(s).
5. For Play Store, configure a private release keystore in Android Studio and build a signed Android App Bundle. Never commit the keystore or passwords.

## 5. Admin
Open `/admin/login.php` (or your mapped admin URL). Sign in with the CLI-created admin. Create a retailer and share their temporary password securely; ask them to change it in a later stage (password-change endpoint is planned).

## 6. API
- `POST /api/login` JSON `{ "phone":"...", "password":"..." }`
- `GET /api/me` with `Authorization: Bearer TOKEN`
- `GET /api/transactions`
- `POST /api/transactions` JSON transaction fields
- `PUT /api/transactions/{id}` JSON transaction fields
- `POST /api/images` multipart: `transaction_id`, `image_type`, `image`

The API binds every transaction query to the authenticated retailer ID. Admin-wide record browsing is a later stage.

## 7. Important hosting note
Do not expose `private_upload_dir` through a public URL. Do not use a public `/uploads` folder for CNIC images. If your host cannot store files outside the public web root, use a separate private storage location or authenticated download controller and verify direct URL access is blocked.
