-- ===========================================
-- SQL Server Setup Script for AccManager
-- Database: AccManager
-- Server: 172.20.235.176
-- ===========================================

-- Create Database
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'AccManager')
BEGIN
    CREATE DATABASE AccManager;
    PRINT 'Database AccManager created successfully.';
END
ELSE
BEGIN
    PRINT 'Database AccManager already exists.';
END

USE AccManager;

-- ===========================================
-- 1. CREATE USERS TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Users')
BEGIN
    CREATE TABLE Users (
        UserId INT PRIMARY KEY IDENTITY(1,1),
        Username NVARCHAR(50) UNIQUE NOT NULL,
        Password NVARCHAR(255) NOT NULL,
        Email NVARCHAR(100),
        FullName NVARCHAR(100),
        Role NVARCHAR(50) NOT NULL,
        Status NVARCHAR(20) DEFAULT 'Active',
        CreatedDate DATETIME DEFAULT GETDATE(),
        LastLogin DATETIME,
        IsActive BIT DEFAULT 1
    );
    PRINT 'Table Users created successfully.';
END

-- ===========================================
-- 2. CREATE APPLICATIONS TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Applications')
BEGIN
    CREATE TABLE Applications (
        AppId INT PRIMARY KEY IDENTITY(1,1),
        Name NVARCHAR(100) NOT NULL,
        Type NVARCHAR(50),
        Status NVARCHAR(20) DEFAULT 'online',
        Icon NVARCHAR(50),
        Description NVARCHAR(500),
        CreatedDate DATETIME DEFAULT GETDATE(),
        UpdatedDate DATETIME DEFAULT GETDATE()
    );
    PRINT 'Table Applications created successfully.';
END

-- ===========================================
-- 3. CREATE ACCOUNTS TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Accounts')
BEGIN
    CREATE TABLE Accounts (
        AccountId INT PRIMARY KEY IDENTITY(1,1),
        UserId INT NOT NULL,
        AppId INT NOT NULL,
        AccountUsername NVARCHAR(100),
        AccountPassword NVARCHAR(255),
        Email NVARCHAR(100),
        AccessLevel NVARCHAR(50),
        Status NVARCHAR(20) DEFAULT 'Active',
        Notes NVARCHAR(MAX),
        CreatedDate DATETIME DEFAULT GETDATE(),
        UpdatedDate DATETIME DEFAULT GETDATE(),
        FOREIGN KEY (UserId) REFERENCES Users(UserId) ON DELETE CASCADE,
        FOREIGN KEY (AppId) REFERENCES Applications(AppId) ON DELETE CASCADE
    );
    PRINT 'Table Accounts created successfully.';
END

-- ===========================================
-- 4. CREATE AUDIT LOG TABLE
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'AuditLog')
BEGIN
    CREATE TABLE AuditLog (
        LogId INT PRIMARY KEY IDENTITY(1,1),
        UserId INT,
        Action NVARCHAR(50),
        TableName NVARCHAR(50),
        RecordId INT,
        OldValue NVARCHAR(MAX),
        NewValue NVARCHAR(MAX),
        Timestamp DATETIME DEFAULT GETDATE(),
        FOREIGN KEY (UserId) REFERENCES Users(UserId)
    );
    PRINT 'Table AuditLog created successfully.';
END

-- ===========================================
-- 5. CREATE INDEXES
-- ===========================================
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Users_Username')
BEGIN
    CREATE INDEX IX_Users_Username ON Users(Username);
END

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Accounts_UserId')
BEGIN
    CREATE INDEX IX_Accounts_UserId ON Accounts(UserId);
END

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_Accounts_AppId')
BEGIN
    CREATE INDEX IX_Accounts_AppId ON Accounts(AppId);
END

PRINT 'Indexes created successfully.';

-- ===========================================
-- 6. INSERT INITIAL DATA
-- ===========================================

-- Check if admin user exists
IF NOT EXISTS (SELECT * FROM Users WHERE Username = 'admin')
BEGIN
    INSERT INTO Users (Username, Password, Email, FullName, Role, Status, IsActive)
    VALUES ('admin', 'admin', 'admin@accmanager.local', 'Administrator', 'admin', 'Active', 1);
    PRINT 'Admin user created: Username=admin, Password=admin';
END

-- Insert sample applications
IF (SELECT COUNT(*) FROM Applications) = 0
BEGIN
    INSERT INTO Applications (Name, Type, Status, Icon, Description)
    VALUES 
        ('AWS', 'Cloud', 'online', 'cloud', 'Amazon Web Services - Cloud Computing'),
        ('GitHub', 'VCS', 'online', 'code', 'GitHub - Version Control System'),
        ('Google Workspace', 'Collaboration', 'online', 'mail', 'Google Workspace - Email and Collaboration'),
        ('Nginx Proxy', 'Infra', 'offline', 'dns', 'Nginx - Web Server and Reverse Proxy');
    PRINT 'Sample applications inserted successfully.';
END

-- ===========================================
-- 7. DISPLAY DATABASE INFORMATION
-- ===========================================
PRINT '';
PRINT '========================================';
PRINT 'DATABASE SETUP COMPLETED SUCCESSFULLY';
PRINT '========================================';
PRINT '';
PRINT 'Database Name: AccManager';
PRINT '';
PRINT 'Tables created:';
SELECT '  - ' + name AS TableName FROM sys.tables ORDER BY name;
PRINT '';
PRINT 'Users in system:';
SELECT '  Username: ' + Username + ' | Role: ' + Role + ' | Status: ' + Status AS UserInfo FROM Users;
PRINT '';
PRINT 'Applications available:';
SELECT '  - ' + Name + ' (' + Type + ') - ' + Status AS AppInfo FROM Applications ORDER BY Name;
PRINT '';
PRINT 'Login Credentials:';
PRINT '  Username: admin';
PRINT '  Password: admin';
PRINT '  Role: admin';
PRINT '';
PRINT '========================================';
