-- init.sql: Create CustomerDB schema, tables, types, procedures, and views.

-- ------------------------------------------------------------
-- Create the database
-- ------------------------------------------------------------
IF DB_ID(N'CustomerDB') IS NULL
BEGIN
    CREATE DATABASE CustomerDB;
END
GO

USE CustomerDB;
GO

-- ------------------------------------------------------------
-- Types / Lookup tables
-- ------------------------------------------------------------
CREATE TABLE dbo.ContactTypes (
    ContactTypeID INT IDENTITY(1,1) PRIMARY KEY,
    TypeName      NVARCHAR(50) NOT NULL UNIQUE
);
GO

INSERT INTO dbo.ContactTypes (TypeName) VALUES
    (N'Phone'),
    (N'Email'),
    (N'Fax'),
    (N'Website');
GO

-- ------------------------------------------------------------
-- Core tables
-- ------------------------------------------------------------
CREATE TABLE dbo.Customers (
    CustomerID   INT IDENTITY(1,1) PRIMARY KEY,
    CustomerName NVARCHAR(150) NOT NULL,
    AddressLine1 NVARCHAR(200) NULL,
    AddressLine2 NVARCHAR(200) NULL,
    City         NVARCHAR(100) NULL,
    State        NVARCHAR(100) NULL,
    ZipCode      NVARCHAR(20)  NULL,
    Country      NVARCHAR(100) NULL,
    CreatedAt    DATETIME2(0)   NOT NULL CONSTRAINT DF_Customers_CreatedAt DEFAULT (SYSDATETIME()),
    UpdatedAt    DATETIME2(0)   NULL
);
GO

CREATE TABLE dbo.Contacts (
    ContactID      INT IDENTITY(1,1) PRIMARY KEY,
    CustomerID     INT NOT NULL,
    ContactTypeID  INT NOT NULL,
    ContactValue   NVARCHAR(200) NOT NULL,
    IsPrimary      BIT NOT NULL CONSTRAINT DF_Contacts_IsPrimary DEFAULT (0),
    CreatedAt      DATETIME2(0) NOT NULL CONSTRAINT DF_Contacts_CreatedAt DEFAULT (SYSDATETIME()),
    UpdatedAt      DATETIME2(0) NULL,
    CONSTRAINT FK_Contacts_Customers FOREIGN KEY (CustomerID)   REFERENCES dbo.Customers(CustomerID)   ON DELETE CASCADE,
    CONSTRAINT FK_Contacts_ContactTypes FOREIGN KEY (ContactTypeID) REFERENCES dbo.ContactTypes(ContactTypeID)
);
GO

-- ------------------------------------------------------------
-- Stored procedures for Customers
-- ------------------------------------------------------------
CREATE PROCEDURE dbo.sp_InsertCustomer
    @CustomerName NVARCHAR(150),
    @AddressLine1 NVARCHAR(200) = NULL,
    @AddressLine2 NVARCHAR(200) = NULL,
    @City        NVARCHAR(100) = NULL,
    @State       NVARCHAR(100) = NULL,
    @ZipCode     NVARCHAR(20)  = NULL,
    @Country     NVARCHAR(100) = NULL,
    @NewCustomerID INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO dbo.Customers (CustomerName, AddressLine1, AddressLine2, City, State, ZipCode, Country)
    VALUES (@CustomerName, @AddressLine1, @AddressLine2, @City, @State, @ZipCode, @Country);

    SET @NewCustomerID = SCOPE_IDENTITY();
END
GO

CREATE PROCEDURE dbo.sp_GetCustomer
    @CustomerID INT
AS
BEGIN
    SET NOCOUNT ON;
    SELECT *
    FROM dbo.Customers
    WHERE CustomerID = @CustomerID;
END
GO

CREATE PROCEDURE dbo.sp_UpdateCustomer
    @CustomerID   INT,
    @CustomerName NVARCHAR(150),
    @AddressLine1 NVARCHAR(200) = NULL,
    @AddressLine2 NVARCHAR(200) = NULL,
    @City        NVARCHAR(100) = NULL,
    @State       NVARCHAR(100) = NULL,
    @ZipCode     NVARCHAR(20)  = NULL,
    @Country     NVARCHAR(100) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE dbo.Customers
    SET CustomerName = @CustomerName,
        AddressLine1 = @AddressLine1,
        AddressLine2 = @AddressLine2,
        City         = @City,
        State        = @State,
        ZipCode      = @ZipCode,
        Country      = @Country,
        UpdatedAt    = SYSDATETIME()
    WHERE CustomerID = @CustomerID;
END
GO

CREATE PROCEDURE dbo.sp_DeleteCustomer
    @CustomerID INT
AS
BEGIN
    SET NOCOUNT ON;
    DELETE FROM dbo.Customers WHERE CustomerID = @CustomerID;
END
GO

-- ------------------------------------------------------------
-- Stored procedures for Contacts
-- ------------------------------------------------------------
CREATE PROCEDURE dbo.sp_InsertContact
    @CustomerID    INT,
    @ContactTypeID INT,
    @ContactValue  NVARCHAR(200),
    @IsPrimary     BIT = 0,
    @NewContactID  INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO dbo.Contacts (CustomerID, ContactTypeID, ContactValue, IsPrimary)
    VALUES (@CustomerID, @ContactTypeID, @ContactValue, @IsPrimary);

    SET @NewContactID = SCOPE_IDENTITY();
END
GO

CREATE PROCEDURE dbo.sp_GetContactsByCustomer
    @CustomerID INT
AS
BEGIN
    SET NOCOUNT ON;
    SELECT c.*, ct.TypeName
    FROM dbo.Contacts c
    JOIN dbo.ContactTypes ct ON c.ContactTypeID = ct.ContactTypeID
    WHERE c.CustomerID = @CustomerID;
END
GO

CREATE PROCEDURE dbo.sp_UpdateContact
    @ContactID     INT,
    @ContactTypeID INT,
    @ContactValue  NVARCHAR(200),
    @IsPrimary     BIT
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE dbo.Contacts
    SET ContactTypeID = @ContactTypeID,
        ContactValue  = @ContactValue,
        IsPrimary     = @IsPrimary,
        UpdatedAt     = SYSDATETIME()
    WHERE ContactID = @ContactID;
END
GO

CREATE PROCEDURE dbo.sp_DeleteContact
    @ContactID INT
AS
BEGIN
    SET NOCOUNT ON;
    DELETE FROM dbo.Contacts WHERE ContactID = @ContactID;
END
GO

-- ------------------------------------------------------------
-- Views
-- ------------------------------------------------------------
CREATE VIEW dbo.vw_CustomerContacts AS
SELECT
    cu.CustomerID,
    cu.CustomerName,
    cu.AddressLine1,
    cu.AddressLine2,
    cu.City,
    cu.State,
    cu.ZipCode,
    cu.Country,
    ct.TypeName,
    co.ContactValue,
    co.IsPrimary
FROM dbo.Customers cu
LEFT JOIN dbo.Contacts co   ON cu.CustomerID = co.CustomerID
LEFT JOIN dbo.ContactTypes ct ON co.ContactTypeID = ct.ContactTypeID;
GO

-- ------------------------------------------------------------
-- Sample data (optional, for quick testing)
-- ------------------------------------------------------------
INSERT INTO dbo.Customers (CustomerName, City, State, Country)
VALUES (N'Acme Corp', N'New York', N'NY', N'USA');

DECLARE @CustID INT = SCOPE_IDENTITY();

EXEC dbo.sp_InsertContact @CustomerID = @CustID,
                         @ContactTypeID = (SELECT ContactTypeID FROM dbo.ContactTypes WHERE TypeName = N'Phone'),
                         @ContactValue = N'555-1234',
                         @IsPrimary = 1,
                         @NewContactID = @CustID OUTPUT;
GO
