-- Work Order Profiles

IF NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = 'wo_profiles')
BEGIN
    CREATE TABLE wo_profiles (
        id                     INT IDENTITY PRIMARY KEY,
        name                   NVARCHAR(200) NOT NULL,
        description            NVARCHAR(MAX),
        category               NVARCHAR(100),
        default_priority       NVARCHAR(10) NOT NULL DEFAULT 'normal',
        default_duration_hours INT,
        default_instructions   NVARCHAR(MAX),
        active                 BIT NOT NULL DEFAULT 1,
        created_at             DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
        updated_at             DATETIME2 NOT NULL DEFAULT GETUTCDATE()
    );
END

IF NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = 'wo_profile_steps')
BEGIN
    CREATE TABLE wo_profile_steps (
        id          INT IDENTITY PRIMARY KEY,
        profile_id  INT NOT NULL REFERENCES wo_profiles(id) ON DELETE CASCADE,
        step_order  INT NOT NULL,
        title       NVARCHAR(200) NOT NULL,
        description NVARCHAR(MAX),
        required    BIT NOT NULL DEFAULT 1
    );

    CREATE INDEX ix_profile_steps ON wo_profile_steps (profile_id, step_order);
END
