using Insight.Database;
using System.Data;

namespace PlanTempus.Database.Tenants
{
    internal class InitializeOrganizationData
    {
        private readonly IDbConnection _db;

        public InitializeOrganizationData(IDbConnection db)
        {
            _db = db;
        }

        private async Task InsertInitialData(string schema)
        {
            // Permission types
            var insertPermissionTypes = $@"
           INSERT INTO {schema}.permission_types (name) VALUES 
           ('NAVIGATION'),
           ('COMMAND'),
           ('VIEW'),
           ('FEATURE')";
            await _db.ExecuteAsync(insertPermissionTypes);

            // Permissions
            var insertPermissions = $@"
           INSERT INTO {schema}.permissions (name, type_id) VALUES 
           -- Navigation permissions
           ('OVERVIEW_VIEW', (SELECT id FROM {schema}.permission_types WHERE name = 'NAVIGATION')),
           ('CALENDAR_VIEW', (SELECT id FROM {schema}.permission_types WHERE name = 'NAVIGATION')),
           ('SALES_VIEW', (SELECT id FROM {schema}.permission_types WHERE name = 'NAVIGATION')),
           ('CUSTOMERS_VIEW', (SELECT id FROM {schema}.permission_types WHERE name = 'NAVIGATION')),

           -- Command permissions
           ('CREATE_PRODUCT', (SELECT id FROM {schema}.permission_types WHERE name = 'COMMAND')),
           ('EDIT_PRODUCT', (SELECT id FROM {schema}.permission_types WHERE name = 'COMMAND')),
           ('DELETE_PRODUCT', (SELECT id FROM {schema}.permission_types WHERE name = 'COMMAND')),
           ('CREATE_CUSTOMER', (SELECT id FROM {schema}.permission_types WHERE name = 'COMMAND')),
           ('EDIT_CUSTOMER', (SELECT id FROM {schema}.permission_types WHERE name = 'COMMAND')),

           -- View permissions
           ('PRODUCT_DETAILS', (SELECT id FROM {schema}.permission_types WHERE name = 'VIEW')),
           ('CUSTOMER_DETAILS', (SELECT id FROM {schema}.permission_types WHERE name = 'VIEW')),
           ('SALES_STATISTICS', (SELECT id FROM {schema}.permission_types WHERE name = 'VIEW')),

           -- Feature permissions
           ('ADVANCED_SEARCH', (SELECT id FROM {schema}.permission_types WHERE name = 'FEATURE')),
           ('EXPORT_DATA', (SELECT id FROM {schema}.permission_types WHERE name = 'FEATURE')),
           ('BULK_OPERATIONS', (SELECT id FROM {schema}.permission_types WHERE name = 'FEATURE'))";
            await _db.ExecuteAsync(insertPermissions);

            // Roles
            var insertRoles = $@"
           INSERT INTO {schema}.roles (name) VALUES
           ('SYSTEM_ADMIN'),
           ('TENANT_ADMIN'),
           ('POWER_USER'),
           ('BASIC_USER')";
            await _db.ExecuteAsync(insertRoles);

            // Top-level navigation
            var insertTopNav = $@"
           INSERT INTO {schema}.navigation_link_templates 
               (parent_id, url, permission_id, icon, default_order)
           VALUES 
               (NULL, '/overview', 
                   (SELECT id FROM {schema}.permissions WHERE name = 'OVERVIEW_VIEW'),
                   'home', 10),
               (NULL, '/sales', 
                   (SELECT id FROM {schema}.permissions WHERE name = 'SALES_VIEW'),
                   'shopping-cart', 20),
               (NULL, '/customers', 
                   (SELECT id FROM {schema}.permissions WHERE name = 'CUSTOMERS_VIEW'),
                   'users', 30)";
            await _db.ExecuteAsync(insertTopNav);

            // Sub-navigation
            var insertSubNav = $@"
           INSERT INTO {schema}.navigation_link_templates 
               (parent_id, url, permission_id, icon, default_order)
           VALUES 
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/sales'), 
                '/sales/create', 
                (SELECT id FROM {schema}.permissions WHERE name = 'CREATE_PRODUCT'),
                'plus', 1),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/customers'), 
                '/customers/create', 
                (SELECT id FROM {schema}.permissions WHERE name = 'CREATE_CUSTOMER'),
                'user-plus', 1)";
            await _db.ExecuteAsync(insertSubNav);

            // Translations for top-level
            var insertTopTranslations = $@"
           INSERT INTO {schema}.navigation_link_template_translations 
               (template_id, language, display_name)
           VALUES 
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/overview'),
                'da-DK', 'Overblik'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/overview'),
                'en-US', 'Overview'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/sales'),
                'da-DK', 'Salg'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/sales'),
                'en-US', 'Sales'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/customers'),
                'da-DK', 'Kunder'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/customers'),
                'en-US', 'Customers')";
            await _db.ExecuteAsync(insertTopTranslations);

            // Translations for sub-navigation
            var insertSubTranslations = $@"
           INSERT INTO {schema}.navigation_link_template_translations 
               (template_id, language, display_name)
           VALUES 
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/sales/create'),
                'da-DK', 'Opret salg'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/sales/create'),
                'en-US', 'Create sale'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/customers/create'),
                'da-DK', 'Opret kunde'),
               ((SELECT id FROM {schema}.navigation_link_templates WHERE url = '/customers/create'),
                'en-US', 'Create customer')";
            await _db.ExecuteAsync(insertSubTranslations);

            // Giv admin alle permissions
            var insertAdminPermissions = $@"
           INSERT INTO {schema}.role_permissions (role_id, permission_id)
           SELECT 
               (SELECT id FROM {schema}.roles WHERE name = 'SYSTEM_ADMIN'),
               id
           FROM {schema}.permissions";
            await _db.ExecuteAsync(insertAdminPermissions);
        }

    }
}
