"""
Copyright 2025 Inmanta

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contact: code@inmanta.com
"""

from asyncpg import Connection


async def update(connection: Connection) -> None:
    """
    Add table that keeps track of the claims that should be included in the tokens
    generated by the login endpoint.
    """
    schema = """
        CREATE TABLE IF NOT EXISTS public.role(
            id UUID NOT NULL,
            name VARCHAR NOT NULL UNIQUE,
            PRIMARY KEY(id)
        );
        CREATE TABLE IF NOT EXISTS public.role_assignment(
            user_id UUID NOT NULL,
            environment UUID NOT NULL,
            role_id UUID NOT NULL,
            PRIMARY KEY(user_id, environment, role_id),
            FOREIGN KEY (environment) REFERENCES public.environment(id) ON DELETE CASCADE,
            FOREIGN KEY (user_id) REFERENCES public.inmanta_user(id) ON DELETE CASCADE,
            FOREIGN KEY (role_id) REFERENCES public.role(id) ON DELETE RESTRICT
        );
    """
    await connection.execute(schema)
