# Installation Guide

This is a comprehensive Real Estate Management System built with Laravel. Follow these steps to set up the application.

## Prerequisites

- PHP 8.1 or higher
- Composer
- Node.js and npm
- MySQL/PostgreSQL database
- Web server (Apache/Nginx) or use `php artisan serve`

## Step 1: Install Laravel Framework

This project requires Laravel to be installed. You can either:

### Option A: Install Laravel first, then copy files
```bash
composer create-project laravel/laravel real-estate-saas
cd real-estate-saas
# Then copy all the files from this project
```

### Option B: Use this as a template
```bash
composer install
php artisan key:generate
```

## Step 2: Install Dependencies

```bash
# PHP dependencies
composer install

# JavaScript dependencies
npm install
```

## Step 3: Publish Spatie Permission

```bash
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
```

## Step 4: Configure Environment

Copy `.env.example` to `.env` (if not exists) and configure your database:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=real_estate_saas
DB_USERNAME=root
DB_PASSWORD=

APP_LOCALE=en
APP_FALLBACK_LOCALE=en

CURRENCY_CODE=TZS
CURRENCY_SYMBOL=TSh
```

## Step 5: Run Migrations and Seeders

```bash
php artisan migrate
php artisan db:seed
```

This will create:
- Database tables
- Default roles (super_admin, company_admin, agent, accountant, tenant, owner)
- Subscription plans (Basic, Pro, Enterprise)
- Super Admin account (admin@realestate.com / password)

## Step 6: Create Storage Link

```bash
php artisan storage:link
```

## Step 7: Build Assets

```bash
npm run build
# Or for development:
npm run dev
```

## Step 8: Start the Server

```bash
php artisan serve
```

Visit: http://127.0.0.1:8000

## Step 9: Register Middleware

Add the custom middleware to `app/Http/Kernel.php` (Laravel 10) or `bootstrap/app.php` (Laravel 11):

### For Laravel 10 (app/Http/Kernel.php):

```php
protected $middlewareGroups = [
    'web' => [
        // ... existing middleware
        \App\Http\Middleware\SetLanguage::class,
        \App\Http\Middleware\EnsureCompanyIsActive::class,
    ],
];
```

### For Laravel 11 (bootstrap/app.php):

```php
->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \App\Http\Middleware\SetLanguage::class,
        \App\Http\Middleware\EnsureCompanyIsActive::class,
    ]);
})
```

## Step 10: Register Service Providers

Ensure Spatie Permission is registered in `config/app.php` (Laravel 10) or it's auto-discovered in Laravel 11.

## Default Login Credentials

**Super Admin:**
- Email: admin@realestate.com
- Password: password

## Next Steps

1. Log in as Super Admin
2. Create companies and manage subscriptions
3. Companies can register through the registration page
4. Each company gets its own isolated data
5. Users are assigned roles with specific permissions

## Troubleshooting

### Permission Denied Errors
- Ensure storage and bootstrap/cache directories are writable
- Run: `chmod -R 775 storage bootstrap/cache`

### Route Not Found
- Run: `php artisan route:clear`
- Run: `php artisan config:clear`
- Run: `php artisan cache:clear`

### Spatie Permission Issues
- Ensure migrations have run: `php artisan migrate`
- Check if roles exist: `php artisan tinker` then `Role::all()`

### Tailwind CSS Not Working
- Run: `npm run build` or `npm run dev`
- Clear browser cache

## Development

For development, use:
```bash
npm run dev
php artisan serve
```

This will enable hot module replacement for assets.
