DROP TABLE IF EXISTS dim_factories;
CREATE TABLE dim_factories (
	factory_sk INTEGER identity(1,1) PRIMARY KEY,
	factory_bk NVARCHAR(100) NOT NULL,
	factory_name NVARCHAR(250),
	factory_country NVARCHAR(250)
);
GO

DROP TABLE IF EXISTS dim_time;
CREATE TABLE dim_time (
	time_sk INTEGER identity(1,1) PRIMARY KEY,
	time_bk DATE NOT NULL,
	time_string NCHAR(10) NOT NULL,
	year INTEGER,
	month INTEGER,
	day INTEGER,
	quarter INTEGER
);
GO

DROP TABLE IF EXISTS dim_drugs;
CREATE TABLE dim_drugs (
	drug_sk INTEGER identity(1, 1) PRIMARY KEY,
	drug_product_id_bk INTEGER NOT NULL, -- business key
	record_status CHAR(1), -- A for active, D for deactivated
	drug_product_name NVARCHAR(250),
	ro_number NVARCHAR(20),
	drug_product_family_id INTEGER,
	drug_product_family_name NVARCHAR(250)
);
GO

DROP TABLE IF EXISTS dim_countries;
CREATE TABLE dim_countries (
	country_sk INTEGER identity(1, 1) PRIMARY KEY,
	country_code NVARCHAR(2), -- business key
	country_name NVARCHAR(250)
);

DROP TABLE IF EXISTS dim_claim_statuses;
CREATE TABLE dim_claim_statuses (
	claim_status_sk INTEGER identity(1, 1) PRIMARY KEY,
	status_code_bk VARCHAR(1) NOT NULL, -- A for Accepted, D for Declined, W for Waiting
	status_description NVARCHAR(50)
);
GO

-- insert statuses
INSERT INTO dim_claim_statuses(status_code_bk, status_description)
VALUES 
	('A', 'Accepted'),
	('D', 'Declined'),
	('W', 'Waiting');
GO

DROP TABLE IF EXISTS dim_indications;
CREATE TABLE dim_indications (
	indication_sk INTEGER identity(1, 1) PRIMARY KEY,
	indication_id_bk INTEGER NOT NULL,
	record_status CHAR(1),
	indication_description NVARCHAR(500)
);
GO

--TABELE FAKTÓW
--to by było do SQL Server'a < 2016
--IF OBJECT_ID('dbo.FT1_Registartion') IS NOT NULL DROP TABLE dbo.MyCategories;


DROP TABLE IF EXISTS FT1_Registration;
CREATE TABLE FT1_Registration (
	id INTEGER identity(1,1) PRIMARY KEY,
	expected_response_time_fk integer FOREIGN KEY REFERENCES dim_time(time_sk),
	submission_date_fk  integer FOREIGN KEY REFERENCES dim_time(time_sk),
	response_time_fk integer FOREIGN KEY REFERENCES dim_time(time_sk), 
	drug_fk integer FOREIGN KEY REFERENCES dim_drugs(drug_sk),
	factory_API_fk integer FOREIGN KEY REFERENCES dim_factories(factory_sk),
	factory_bulk_fk integer FOREIGN KEY REFERENCES dim_factories(factory_sk),
	factory_package_fk integer FOREIGN KEY REFERENCES dim_factories(factory_sk),
	country_fk integer FOREIGN KEY REFERENCES dim_countries(country_sk), 
	--claim status do uspójnienia z tabelą dim_status, wydaje mi się, że można by dodać przedrostek claim do kolumn, będzie czytelniej :) 
	claim_status_fk integer FOREIGN KEY REFERENCES dim_claim_statuses(claim_status_sk), 
	indication_fk integer FOREIGN KEY REFERENCES dim_indications(indication_sk),
	claim_number NVARCHAR(50), 
	-- cnt jest tylko zliczeniowy, będzie miał 1, więc dałbym tinyint
	cnt tinyint,
);
GO

DROP TABLE IF EXISTS FT2_Refund;
CREATE TABLE FT2_Refund (
	id INTEGER identity(1,1) PRIMARY KEY,
	claim_status_fk integer FOREIGN KEY REFERENCES dim_claim_statuses(claim_status_sk),
	drug_fk integer FOREIGN KEY REFERENCES dim_drugs(drug_sk),
	submission_date_fk integer FOREIGN KEY REFERENCES dim_time(time_sk),
	expected_response_fk integer FOREIGN KEY REFERENCES dim_time(time_sk),
	response_time_fk integer FOREIGN KEY REFERENCES dim_time(time_sk), 
	country_fk integer FOREIGN KEY REFERENCES dim_countries(country_sk), 
	indication_fk integer FOREIGN KEY REFERENCES dim_indications(indication_sk), 
	registration_country_fk integer FOREIGN KEY REFERENCES dim_countries(country_sk), 
	--decimal zjada mniej, niż money, do przedyskutowania (precyzja - liczba miejsc przed i po przecinku, skala - liczba miejsc po przecinku)
	price money,
	reimbursement_amountPercent smallint, 
	--tu obliczymy jako iloczyn price * reimbursement_amountPercent
	reimbursement_amount decimal(9,2),
	claim_number integer,
	cnt tinyint,
);
GO