<?php

return <<<'SQL'
CREATE TABLE support_requests (
    id CHAR(36) NOT NULL PRIMARY KEY,
    tenant_id CHAR(36) NOT NULL,
    request_number VARCHAR(40) NOT NULL,
    request_direction VARCHAR(50) NOT NULL DEFAULT 'member_to_tenant',
    route_target VARCHAR(50) NOT NULL DEFAULT 'tenant_responsible',
    category VARCHAR(80) NOT NULL DEFAULT 'general',
    priority VARCHAR(20) NOT NULL DEFAULT 'normal',
    status VARCHAR(50) NOT NULL DEFAULT 'new',
    subject VARCHAR(255) NOT NULL,
    message TEXT NOT NULL,
    requester_user_id CHAR(36) NULL,
    requester_tenant_user_id CHAR(36) NULL,
    requester_name VARCHAR(255) NOT NULL,
    requester_email VARCHAR(255) NOT NULL,
    assigned_to_user_id CHAR(36) NULL,
    assigned_to_tenant_user_id CHAR(36) NULL,
    last_message_at DATETIME NOT NULL,
    resolved_at DATETIME NULL,
    closed_at DATETIME NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY support_requests_tenant_number_unique (tenant_id, request_number),
    KEY support_requests_tenant_status_index (tenant_id, status),
    KEY support_requests_tenant_direction_index (tenant_id, request_direction),
    KEY support_requests_requester_index (requester_user_id),
    KEY support_requests_assignee_index (assigned_to_user_id),
    FOREIGN KEY (tenant_id) REFERENCES tenants(id),
    FOREIGN KEY (requester_user_id) REFERENCES users(id),
    FOREIGN KEY (requester_tenant_user_id) REFERENCES tenant_users(id),
    FOREIGN KEY (assigned_to_user_id) REFERENCES users(id),
    FOREIGN KEY (assigned_to_tenant_user_id) REFERENCES tenant_users(id)
);
SQL;
