-- ============================================================================
-- c-entron.NET - Complete Database Schema Export Script
-- ============================================================================
-- This script exports the complete database schema including:
-- - All tables with column definitions
-- - Data types and constraints
-- - Nullable flags and defaults
-- - Foreign key relationships
-- - Index information
--
-- Generated: 2025-11-11
-- ============================================================================

SET NOCOUNT ON

-- ============================================================================
-- SECTION 1: TABLE SUMMARY - COUNT OF TABLES AND COLUMNS
-- ============================================================================

PRINT '=== SECTION 1: DATABASE OVERVIEW ==='
PRINT ''
PRINT 'Total Tables: '
SELECT COUNT(DISTINCT TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo'

PRINT ''
PRINT 'Total Columns: '
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo'

PRINT ''
PRINT 'Tables by Column Count:'
SELECT TOP 50
    TABLE_NAME,
    COUNT(*) AS ColumnCount
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo'
GROUP BY TABLE_NAME
ORDER BY ColumnCount DESC

PRINT ''
PRINT '=== SECTION 2: COMPLETE TABLE AND COLUMN SCHEMA ==='
PRINT ''

-- ============================================================================
-- SECTION 2: COMPLETE SCHEMA WITH ALL COLUMNS
-- ============================================================================

SELECT
    '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' AS 'SCHEMA'
UNION ALL
SELECT 'TABLE: ' + t.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_SCHEMA = 'dbo'
ORDER BY t.TABLE_NAME
UNION ALL
SELECT
    '  ' +
    COLUMN_NAME +
    ' [' + DATA_TYPE +
    CASE
        WHEN CHARACTER_MAXIMUM_LENGTH > 0
        THEN '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(20)) + ')'
        WHEN CHARACTER_MAXIMUM_LENGTH = -1
        THEN '(MAX)'
        ELSE ''
    END +
    '] ' +
    CASE WHEN IS_NULLABLE = 'NO' THEN 'NOT NULL' ELSE 'NULL' END +
    CASE WHEN COLUMN_DEFAULT IS NOT NULL THEN ' = ' + COLUMN_DEFAULT ELSE '' END AS 'SCHEMA'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo'
ORDER BY TABLE_NAME, ORDINAL_POSITION

-- ============================================================================
-- SECTION 3: FOREIGN KEY RELATIONSHIPS
-- ============================================================================

PRINT ''
PRINT '=== SECTION 3: FOREIGN KEY RELATIONSHIPS ==='
PRINT ''

SELECT
    'Table: ' + OBJECT_NAME(fk.parent_object_id) AS 'Relationship',
    (SELECT NAME FROM sys.columns WHERE object_id = fk.parent_object_id AND column_id = fk.parent_column_id) AS 'FK Column',
    (SELECT NAME FROM sys.tables WHERE object_id = fk.referenced_object_id) AS 'References Table',
    (SELECT NAME FROM sys.columns WHERE object_id = fk.referenced_object_id AND column_id = fk.referenced_column_id) AS 'References Column'
FROM sys.foreign_keys fk
WHERE fk.database_id = DB_ID()
ORDER BY OBJECT_NAME(fk.parent_object_id), fk.name

-- ============================================================================
-- SECTION 4: PRIMARY KEYS
-- ============================================================================

PRINT ''
PRINT '=== SECTION 4: PRIMARY KEY DEFINITIONS ==='
PRINT ''

SELECT
    t.name AS 'Table Name',
    c.name AS 'Primary Key Column',
    i.name AS 'Index Name',
    i.type_desc AS 'Index Type'
FROM sys.tables t
JOIN sys.indexes i ON t.object_id = i.object_id AND i.is_primary_key = 1
JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
JOIN sys.columns c ON t.object_id = c.object_id AND ic.column_id = c.column_id
WHERE t.schema_id = SCHEMA_ID('dbo')
ORDER BY t.name

-- ============================================================================
-- SECTION 5: ALL INDEXES
-- ============================================================================

PRINT ''
PRINT '=== SECTION 5: INDEX DEFINITIONS ==='
PRINT ''

SELECT
    SCHEMA_NAME(t.schema_id) AS 'Schema',
    t.name AS 'Table Name',
    i.name AS 'Index Name',
    i.type_desc AS 'Index Type',
    STRING_AGG(c.name, ', ') WITHIN GROUP (ORDER BY ic.key_ordinal) AS 'Columns'
FROM sys.tables t
JOIN sys.indexes i ON t.object_id = i.object_id
JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
JOIN sys.columns c ON t.object_id = c.object_id AND ic.column_id = c.column_id
WHERE t.schema_id = SCHEMA_ID('dbo')
GROUP BY SCHEMA_NAME(t.schema_id), t.name, i.name, i.type_desc
ORDER BY t.name, i.name

-- ============================================================================
-- SECTION 6: TABLES BY FUNCTIONAL AREA (based on naming patterns)
-- ============================================================================

PRINT ''
PRINT '=== SECTION 6: TABLES GROUPED BY FUNCTIONAL AREA ==='
PRINT ''

SELECT
    CASE
        WHEN TABLE_NAME LIKE 'Account%' THEN 'Account Management'
        WHEN TABLE_NAME LIKE 'Artikel%' OR TABLE_NAME LIKE 'ARTIK%' THEN 'Articles & Products'
        WHEN TABLE_NAME IN ('AngKopf','AngPos','AufKopf','AufPos','BestKopf','BestPos') THEN 'Orders & Sales'
        WHEN TABLE_NAME IN ('RechKopf','RechPos','LiefKopf','LiefPos') THEN 'Transactions (Orders/Invoices)'
        WHEN TABLE_NAME LIKE 'hlpdsk_%' OR TABLE_NAME LIKE 'Ticket%' THEN 'Helpdesk & Ticketing'
        WHEN TABLE_NAME LIKE 'AssetManagement%' THEN 'Asset Management'
        WHEN TABLE_NAME LIKE 'EDI%' THEN 'EDI & Integration'
        WHEN TABLE_NAME LIKE 'Inventur%' OR TABLE_NAME LIKE 'Lager%' THEN 'Inventory & Warehouse'
        WHEN TABLE_NAME IN ('Personal','Abteilung','Arbeitsplatz','PersonalUrlaub') THEN 'Employee & HR'
        WHEN TABLE_NAME LIKE 'BookKeeping%' OR TABLE_NAME LIKE 'Buchhalter%' THEN 'Financial & Accounting'
        WHEN TABLE_NAME LIKE 'Campaign%' OR TABLE_NAME LIKE 'CRM%' THEN 'CRM & Campaigns'
        WHEN TABLE_NAME LIKE 'CentronDMS%' OR TABLE_NAME LIKE 'Document%' THEN 'Document Management'
        WHEN TABLE_NAME LIKE 'Report%' OR TABLE_NAME LIKE 'Cache%' OR TABLE_NAME LIKE 'Statistic%' THEN 'Reporting & Analytics'
        WHEN TABLE_NAME LIKE 'Web%' OR TABLE_NAME LIKE 'WEB%' THEN 'Web & Portal'
        WHEN TABLE_NAME LIKE 'Centron%' OR TABLE_NAME LIKE 'Application%' OR TABLE_NAME LIKE 'Module%' THEN 'System & Configuration'
        ELSE 'Other'
    END AS 'Functional Area',
    TABLE_NAME AS 'Table Name',
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = INFORMATION_SCHEMA.TABLES.TABLE_NAME) AS 'Column Count'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
ORDER BY 'Functional Area', 'Table Name'

-- ============================================================================
-- SECTION 7: STANDARD COLUMNS AUDIT
-- ============================================================================

PRINT ''
PRINT '=== SECTION 7: STANDARD COLUMNS COMPLIANCE AUDIT ==='
PRINT ''
PRINT 'Tables with all required standard columns (I3D, CreatedByI3D, CreatedDate, ChangedByI3D, ChangedDate):'
PRINT ''

SELECT DISTINCT
    t.TABLE_NAME,
    CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'I3D') THEN 'Yes' ELSE 'NO' END AS 'Has_I3D',
    CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'CreatedByI3D') THEN 'Yes' ELSE 'NO' END AS 'Has_CreatedByI3D',
    CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'CreatedDate') THEN 'Yes' ELSE 'NO' END AS 'Has_CreatedDate',
    CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'ChangedByI3D') THEN 'Yes' ELSE 'NO' END AS 'Has_ChangedByI3D',
    CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'ChangedDate') THEN 'Yes' ELSE 'NO' END AS 'Has_ChangedDate',
    CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'IsDeleted') THEN 'Yes' ELSE 'NO' END AS 'Has_IsDeleted'
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_SCHEMA = 'dbo'
ORDER BY t.TABLE_NAME

-- ============================================================================
-- SECTION 8: TABLES WITH EXTENDED/VERSION VARIANTS
-- ============================================================================

PRINT ''
PRINT '=== SECTION 8: TABLES WITH VERSION AND EXTENDED VARIANTS ==='
PRINT ''

SELECT DISTINCT
    CASE
        WHEN TABLE_NAME LIKE '%Versions' THEN SUBSTRING(TABLE_NAME, 1, LEN(TABLE_NAME) - 8) + ' (Versions)'
        WHEN TABLE_NAME LIKE '%Erweitert' THEN SUBSTRING(TABLE_NAME, 1, LEN(TABLE_NAME) - 8) + ' (Extended)'
        ELSE TABLE_NAME
    END AS 'Base Table',
    TABLE_NAME AS 'Actual Table Name',
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = INFORMATION_SCHEMA.TABLES.TABLE_NAME) AS 'Columns'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND (TABLE_NAME LIKE '%Versions' OR TABLE_NAME LIKE '%Erweitert')
ORDER BY 'Base Table', 'Actual Table Name'

-- ============================================================================
-- SECTION 9: COLUMN NAME PATTERNS ANALYSIS
-- ============================================================================

PRINT ''
PRINT '=== SECTION 9: FOREIGN KEY PATTERN ANALYSIS ==='
PRINT ''
PRINT 'Columns ending with I3D (presumed foreign keys):'
PRINT ''

SELECT
    TABLE_NAME,
    COLUMN_NAME,
    COUNT(*) OVER (PARTITION BY COLUMN_NAME) AS 'Times Used'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo' AND COLUMN_NAME LIKE '%I3D'
ORDER BY COLUMN_NAME, TABLE_NAME

-- ============================================================================
-- SECTION 10: DATA TYPE DISTRIBUTION
-- ============================================================================

PRINT ''
PRINT '=== SECTION 10: DATA TYPE DISTRIBUTION ==='
PRINT ''

SELECT
    DATA_TYPE,
    COUNT(*) AS 'Occurrences',
    COUNT(DISTINCT TABLE_NAME) AS 'Tables Using This Type'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo'
GROUP BY DATA_TYPE
ORDER BY 'Occurrences' DESC

-- ============================================================================
-- END OF SCHEMA EXPORT
-- ============================================================================

PRINT ''
PRINT '=== EXPORT COMPLETE ==='
PRINT 'Export timestamp: ' + FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm:ss')
PRINT ''
