# Stage 09 - Initial Tenant

## Goal

This stage adds the first tenant record for the domain where the installer runs.

## Files

Created:

- `installer-package/install/src/TenantStep.php`

Changed:

- `installer-package/install/bootstrap.php`
- `installer-package/install/install.php`
- `installer-package/install/src/InstallerState.php`
- `installer-package/install/src/RequirementStep.php`
- `installer-package/install/views/partials/main-form.php`

## Form Fields

The first tenant section now asks for:

- Tenant key
- Company name
- Site name
- Tenant DB name
- Domain
- Theme
- Language

Default values:

- Tenant key: `bessoft`
- Company name: `Bes Soft`
- Site name: `Bes Web Demo`
- Tenant DB name: `bessoftc_besweb_bessoft`
- Theme: `default`
- Language: `turkish`

## Database Behavior

The installer writes the tenant metadata into the master database:

- `tenants`
- `tenant_domains`

The domain input is normalized before saving. If the user enters `https://www.bessoft.net/path`, the base domain becomes `bessoft.net`.

Domain rows created:

- `bessoft.net` as primary
- `www.bessoft.net` as secondary, when the domain is not localhost or an IP address

## Idempotent Rules

The action can be run more than once.

- If the tenant key does not exist, a new tenant is inserted.
- If the tenant key exists, base tenant information is updated.
- Existing domain rows for the same tenant are updated to active.
- A domain already owned by another tenant blocks the installation.

## Security Notes

- Tenant key is restricted to lowercase letters, numbers, dash and underscore.
- Tenant DB name is restricted to letters, numbers and underscore.
- Tenant DB password is stored only in the master DB record; it is not printed in reports.
- Tenant physical DB creation is not performed in this stage.

## Next Stage

Stage 10 will create or prepare the tenant database schema and seed the default demo site content.