drop table if exists coupon_campaigns cascade;
create table coupon_campaigns(
	coupon_campaign_id serial,
	cc_name varchar(255),
	cc_number_of_coupons integer default 0,
	cc_avail_from date default CURRENT_DATE,
	cc_avail_until date,
	cc_coupon_type varchar(16) default 'freedays',
	cc_freedays integer default 0,
	constraint coupon_campaigns_coupon_campaign_id_pkey primary key( coupon_campaign_id )
);

comment on table coupon_campaigns is 'storage of coupon campaigns';
comment on column coupon_campaigns.coupon_campaign_id is 'unique id of table';
comment on column coupon_campaigns.cc_name is 'name of the coupon campaign';
comment on column coupon_campaigns.cc_number_of_coupons is 'number of coupons in the campaign';
comment on column coupon_campaigns.cc_avail_from is 'campaign available from date';
comment on column coupon_campaigns.cc_avail_until is 'campaign available until date';
comment on column coupon_campaigns.cc_coupon_type is 'type of coupon (freedays)';
comment on column coupon_campaigns.cc_freedays is 'freedays given (if type of coupon is freedays)'; 

drop table if exists coupon_tickets;
create table coupon_tickets(
	coupon_ticket_id serial,
	coupon_campaign_id integer not null references coupon_campaigns( coupon_campaign_id ),
	coupon_code varchar(16),
	used_at timestamp without time zone,
	user_id integer references users( user_id ),
	constraint coupon_tickets_coupon_ticket_id_pkey primary key( coupon_ticket_id )
);

comment on table coupon_tickets is 'storage of coupon tickets';
comment on column coupon_tickets.coupon_ticket_id is 'unique id of table';
comment on column coupon_tickets.coupon_campaign_id is 'reference id to campaigns table';
comment on column coupon_tickets.coupon_code is 'the coupon code itself';
comment on column coupon_tickets.used_at is 'time stamp of usage (if used)';
comment on column coupon_tickets.user_id is 'user of the coupon';
